Editing Marshalling updates to a text box
Jump to:
navigation
,
search
When you have a text box on a form, it's typically created as part of the form's initialization code, in the main thread. If you have a background thread that needs to update the text box, you can't just "pass the text box into the thread", nor can you even respond to an Event in the thread with a handler and "just update the text box" (the event handler runs in the thread of the event). The code isn't difficult to write, but it is ''complex'', in that it's not intuitive nor easy to simply type out when you need it. Delegate Sub AddLogtext_Callback(TB As TextBox, Text As String) ' Private MyTB As TextBox = Nothing ' Sub AddLogText_Invoke(TB As TextBox, Text As String) If TB.InvokeRequired Then Dim AddLogText_Delegate As New AddLogtext_Callback(AddressOf AddLogText_Invoke) MyTB = TB Dim ParmArray() As Object = {MyTB, Text} MyTB.Invoke(AddLogText_Delegate, ParmArray) Else tbInfo.Text &= Format(Now, "hh:mm:ss tt") & " " & Text & vbCrLf tbInfo.Select(tbInfo.TextLength, 1) tbInfo.ScrollToCaret() Application.DoEvents() End If End Sub ' Sub LogInfo(Text As String) _TextIsDirty = True AddLogText_Invoke(tbInfo, Text) End Sub '''NOTE: The code below isn't right either! Be sure to model after the above code''' Private Delegate Sub ShowText(Text As String) ' Sub ShowText_Invoke(Text As String) If tbActionLog.InvokeRequired Then Dim SHowText_Delegate As New ShowText(AddressOf ShowText_Invoke) SHowText_Delegate(Text) Else tbActionLog.Text &= Format(Now, "hh:mm:ss tt") & " " & Text & vbCrLf tbActionLog.Select(tbActionLog.TextLength, 1) tbActionLog.ScrollToCaret() Application.DoEvents() End If End Sub
|
Editing help
Personal tools
Create account
Log in
Namespaces
Page
Discussion
Variants
Views
Read
Edit
View history
Actions
Search
Navigation
Main page
Community portal
Current events
Recent changes
Random page
Help
Toolbox
What links here
Related changes
Special pages