|
-
Mar 3rd, 2011, 02:30 PM
#1
Thread Starter
Addicted Member
[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.
-
Mar 3rd, 2011, 03:29 PM
#2
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.
-
Mar 4th, 2011, 09:24 AM
#3
Thread Starter
Addicted Member
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.
-
Mar 4th, 2011, 09:36 AM
#4
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.
-
Mar 4th, 2011, 09:39 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|