Updating a textbox from another class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a main form for my project. It uses a routine in another class to do
work on a file -- line by line. I'd like to have a text box on my form
display the lines, say every 5,000 -- so I can tell the progress. From what
LITTLE I know it looks like I'd have to pass the form to the class so that
the class can refer back to the form and update the textbox. It seems to me
that there must be a better way to handle this. Can anyone tell me the right
way to do this?

Art
 
Art,
Besides passing the entire form object to the class, you could just
pass the TextBox control to the class. Another way you can do it is by
using a delegate function. Here is a quick example:

Public Class Updater

Delegate Sub MyCallbackRoutine(ByVal Buffer As String)

Public Sub MyRoutine(Optional ByVal Callback As
MyCallbackRoutine = Nothing)

If Not (Callback Is Nothing) Then
Callback("This message gets sent to the delegate
function.")
End If

End Sub

End Class

Now on your form that calls the class, you would create a function that
follows the same parameter syntax of the delegate:

Private Sub UpdateTextBox(ByVal Buffer As String)

TextBox1.Text = Buffer

End Sub

And in the event/function that calls the method in your class, you
would do this:

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStart.Click

' Declare and Instantiate the class object.
Dim oUpdate As New Updater

' Declare and Instantiate the delegate.
Dim oCallback As New Updater.MyCallbackRoutine(AddressOf
UpdateTextBox)

' Call the method within the class and pass the delegate
object.
oUpdate.MyRoutine(oCallback)

End Sub

If you step through the code, you will see that the "oUpdate.MyRoutine"
function will call the "UpdateTextBox" method because you are passing
in the delegate object that contains the address of that function.

I hope this helps (and I hope I explained it correctly).
Regards,
David R. Jenkins
 
Use an event to alert the form of the status, that separates the processing
from the class instance

'For example, in your form:

Dim WithEvents m_prc As ProcessData = New ProcessData
Private Sub m_prc_Status(ByVal counter As Integer) Handles m_prc.Status

TextBox1.Text = counter.ToString

End Sub

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

m_prc.Execute()

End Sub

' In the class definition:

Public Class ProcessData

Public Event Status(ByVal counter As Integer)

Public Sub Execute()

For i As Integer = 0 To 5000000

If i Mod 5000 = 0 Then

RaiseEvent Status(i)

End If

Next

End Sub

End Class
 
David,

Thank you very much. I've had no experience using Delegates so this should
be interesting!

Art
 
Jim,

Thanks for the help. I'm still kind of new at this and even though I have
generated events before, I'm not yet at home with all of this.

Art
 

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