Set Label Text From Background Worker

WebKill2k

Member
Joined
May 15, 2008
Messages
14
Programming Experience
1-3
I ran into the problem of not being able to set a labels text from a background worker thread and came across the way to do it, I modified the sub (which only worked for a specified control) to try and make it work with any control I specify, but I get parameter count missmatch on Me.Invoke(d, New Object() {[text]})

VB.NET:
    Delegate Sub SetTextCallback(ByVal [text] As String, ByVal lblControl As Label)

    Private Sub SetLabelText(ByVal [text] As String, ByVal lblControl As Label)
        If lblControl.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetLabelText)
            Me.Invoke(d, New Object() {[text]})
        Else
            lblControl.Text = [text]
        End If
    End Sub

Still unfamiliar and learning about how to do this, so any help would be appreciated.
 
If you're only setting 1 control you can use the ReportProgress method of the BW and use the UserState object to pass the string.

That way you don't have to delegate anything, it handles that for you via the method and event.
 
Back
Top