Results 1 to 5 of 5

Thread: [RESOLVED] How to change Label name from BackgroundWorker?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Resolved [RESOLVED] How to change Label name from BackgroundWorker?

    I just want to to change a label name from the backgroundworker, the code is plain simple:

    Code:
    label1.text = WebBrowser1.DocumentTitle
    and the error returned would be "Specified cast is not valid".

    Now running the code from a button would do the job, but from the bgworker not, and I can't seem to get around it.

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: How to change Label name from BackgroundWorker?

    The first note (marked in yellow) in the helpfile tells you which events you can access UI components. Have a read to help you understand why you are getting the problem.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Re: How to change Label name from BackgroundWorker?

    honestly, the note file just tells me I can't do it that way, not more than that.

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: How to change Label name from BackgroundWorker?

    Maybe you are not reading the note, I'll copy out the bit I mean to help you:

    Note
    You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the ProgressChanged and RunWorkerCompleted events.

  5. #5
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: How to change Label name from BackgroundWorker?

    Try This
    Code:
        Delegate Sub SetLabelTextInvoker(ByVal label As Label, ByVal Text As String)
        Sub SetLabelText(ByVal Label As Label, ByVal Text As String)
            If Label.InvokeRequired = True Then
                Label.Invoke(New SetLabelTextInvoker(AddressOf SetLabelText), Label, Text)
            Else
                Label.Text = Text
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            BackgroundWorker1.RunWorkerAsync()
    
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            SetLabelText(Label1, "This text Was set From A background worker")
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width