Reputation: 355
I'm trying to change the text of a label located in Form1
, the form contain this:
Private Sub BKW_DoWork(sender As Object, e As DoWorkEventArgs) Handles BKW.DoWork
MyFunction() ' This function is located in the class 'General function
End Sub
in the same Form1
I also have:
Private Delegate Sub UpdateLabelDel(ByVal txt As String)
Public Sub UpdateLabel(ByVal txt As String)
If Me.InvokeRequired Then
Invoke(New UpdateLabelDel(AddressOf UpdateLabel), txt)
Else
Label1.Text = txt
End If
End Sub
in the GeneralFunction
class I have this code for change the text label:
Dim frm As New Form1
frm.UpdateLabel("Starting function")
How you can see I call MyFunction()
from BackGroundWorker
called BKW
, this function update the text using .UpdateLabel
localted in Form1. The problem is that the Label1
located in the same Form1 doesn't change any text, what is wrong?
Upvotes: 0
Views: 1307
Reputation: 219047
This creates a new form:
Dim frm As New Form1
So you're not changing the text on the form that you see, you're changing it on an entirely separate form that you haven't displayed.
Instead, pass a reference to the current form to the code that needs it. Something like this:
MyFunction(Me)
And the function signature would expect that reference, something like this:
Public Sub MyFunction(ByVal form As Form1)
And in that function you can reference the form instance from that variable:
form.UpdateLabel("Starting function")
Essentially, if you have code (classes, methods, etc.) which needs a reference to an object, the code which calls it should supply it with that reference.
Upvotes: 2