Question How to Update the Form label from the Class file?

suresh_ed

Active member
Joined
May 19, 2008
Messages
27
Programming Experience
1-3
I have a Windows Form1 with label and i want to update the Form label text from the class file.
I can able to get the Form1 controls from the class file but it is not updating the value in the Form.

VB.net

Below is the Form1:
VB.NET:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim obj As New Class1()
    End Sub
End Class


Below is my Class:
VB.NET:
Public Class Class1
    Public Sub New()
        Dim frm As New Form1
        My.Application.DoEvents()
        frm.Refresh()
        frm.Label1.Refresh()
        frm.Label1.Text = "Updation from Class1"
        frm.Label1.Refresh()
        frm.Refresh()
        My.Application.DoEvents()
    End Sub
End Class

After the execution of code also it is not updating the label text to "Updation from Class1".
Any help is appreciated.

Regards,
Suresh
 
In the Constructor Code of Class1 you wrote in the first line
VB.NET:
 Dim frm As New Form1
which means frm as a new form object not a reference to your current form, what you need to do for your code to work is to comment that line and change the Name property in the designer for Form1 into frm
 
Back
Top