Difference between revisions of "Marshalling updates to a text box"

From RayHarwood
Jump to: navigation, search
(Created page with "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...")
 
 
Line 2: Line 2:
  
 
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.
 
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

Latest revision as of 15:09, 29 April 2016

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox