473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Updating a textbox from another class

Art
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
Nov 21 '05 #1
4 3514
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

Nov 21 '05 #2
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

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.com...
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

Nov 21 '05 #3
Art
David,

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

Art
Nov 21 '05 #4
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
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Tim | last post by:
Hi A while ago I got some help in writing a mock console. The idea was to write a VB app that contained a textbox, then run a process from within the app and redirect the stdout to the textbox. ...
5
by: CCLeasing | last post by:
Hello, I have searched google but can not find a straight forward answer to my problem. Hopefuly someone will be kind enough to offer their expertise. Please forgive if this seems a bit convoluted...
4
by: Peted | last post by:
Hello i have a textbox on a form1. I want to pass the name of that textbox as a paramter to another class (not a form class) and once gotten this paramter i want to update the textbox on...
2
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi All! I am with a situation where I am not getting the right updating to the form's fields. The situation is the following one: I have one combobox and one textbox. I am using the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.