Reference a Control from a Class

  • Thread starter Thread starter wg
  • Start date Start date
W

wg

I am experienced in VB but sort of new to VB.NET. I have written a Error
Logger Class that will create a text log file and store errors and messages.
I am now attempting to use the same class to update a listbox on the form.
So far I have been unsuccessful in accessing anything on the form from a
class.

Any help would be greatly appreciated.

Thanks

wg
 
wg said:
I am experienced in VB but sort of new to VB.NET. I have written a Error
Logger Class that will create a text log file and store errors and
messages.
I am now attempting to use the same class to update a listbox on the form.
So far I have been unsuccessful in accessing anything on the form from a
class.

Your class needs a reference to the control in order to access it. You can
add a property to your class that stores the output control. This property
can be set when the class is instantiated. Alternatively you can raise an
event whenever a log message is added and add a handler to this event in
your form. Inside the handler you update the control.
 
When you refrence a new class you must create an instance of it.


dim myClass as new ErrorLoggerClass

myClass.method()
myClass.propertie = "xxx"

etc etc
 
This can be one of the ways:

- Create an event to raise in your class.
- Pass the Form as an reference to the event.

Then you can access the ListBox in the form.

chanmm
 
I guess I am missing something. How do I reference the control from within
the class?

Thanks

wg
 
Give you one example.

Define an Interface in your solution:

Interface ICallback
Sub CallbackMethod(ByVal str As String)
End Interface

Define a class:

Friend Class Class1

Private icb As ICallback

Public Sub RegisterInterFaceForCallback(ByVal cb As ICallback)
'this method receives a reference to the client.

'Register the client for the callback
icb = cb

End Sub

Public Sub UnRegisterInterfaceForCallback()

'Unregister the client
icb = Nothing

End Sub

Public Sub UseInterfaceCallback()

'Make sure something is registered first
If Not icb Is Nothing Then
'Call back into the client code
icb.CallbackMethod("Item1")
End If

End Sub
End Class

The From you have. Implements the ICallBack Interface:

Public Class Form1
Inherits System.Windows.Forms.Form

Implements ICallback

Public Sub CallbackMethod(ByVal str As String) Implements
ICallback.CallbackMethod
'This method is called from Class1 after the form is registered
'with the instance.

ListBox1.Items.Add(str)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim refClass1 As New Class1

'Register the client, passing a reference to itself
refClass1.RegisterInterFaceForCallback(Me)

'Call the method which will in turn call back into
'the client
refClass1.UseInterfaceCallback()

'Unregister the client
refClass1.UnRegisterInterfaceForCallback()
End Sub
End Class

Hope this get you a better idea.

chanmm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top