'Getting the value of the label from other form using vb.net

It is not working, the labels only goes back into its default values. What do you think is the problem?

Okay this is my code:

Actually I'm using mysql as my database here

This is the form that generates the values of the labels:

Private Sub ProfileControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
        Dim conn As New MySqlConnection(ServerString)
        Dim dap As New MySqlDataAdapter("select * from employee where LogInID = '" & Main.ID.Text & "'", conn)
        Dim dt As New DataTable
        dap.Fill(dt)

        employeenum = dt.Rows(0).Item("EmployeeID")
        position = dt.Rows(0).Item("Position")
        employeename = dt.Rows(0).Item("FirstName") + " " + dt.Rows(0).Item("LastName")

        lblemployeename.Text = employeename
        lblemployeenum.Text = employeenum
        EmpPosition.Text = position

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

And this is the form that will retrieve the values of the 3 labels.

Private Sub addsavebutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addsavebutton.Click
    Dim profile As New ProfileControl


    If txtbranchname.Text <> "" Then
        If addsavebutton.Text = "ADD" Then

            Dim zero As Integer = 0
            Dim SQLStatement As String = "INSERT INTO branch(BranchName,Centers)VALUES('" & txtbranchname.Text & "','0') "
            SaveCenter(SQLStatement)
            logdate = Convert.ToDateTime(Date.Now).ToString("yyyy-MM-dd hh:mm:ss")
            logdate2 = Format(Date.Now, "yyyy-MM-dd")
            status = "Added Branch " + txtbranchname.Text
            SQLStatement = "INSERT INTO log(EmployeeID,Name,EmployeePosition,Date,DateTime,Status)VALUES('" & profile.lblemployeenum.Text & "','" & profile.lblemployeename.Text & "','" & profile.EmpPosition.Text & "','" & logdate2 & "','" & logdate & "','" & status & "' )"
            Savelog(SQLStatement)
            txtbranchname.Clear()
        ElseIf addsavebutton.Text = "SAVE" Then
            Dim Query As String

            Dim con As MySqlConnection = New MySqlConnection(ServerString)
            con.Open()

            Query = "UPDATE branch SET  BranchName = '" & txtbranchname.Text & "' WHERE BranchCode = '" & txtbranchcode.Text & "'"


            Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
            Dim i As Integer = cmd.ExecuteNonQuery()
            If (i > 0) Then

                'success
                Dim Successtext As New MsgSuccess
                Successtext.PassedText = "Record is Successfully Updated"
                Successtext.ShowDialog()


                Dim SQLStatement As String
                logdate = Convert.ToDateTime(Date.Now).ToString("yyyy-MM-dd hh:mm:ss")
                logdate2 = Format(Date.Now, "yyyy-MM-dd")
                status = "Updated Branch: " + txtbranchcode.Text + ", " + txtbranchname.Text
                SQLStatement = "INSERT INTO log(EmployeeID,Name,EmployeePosition,Date,DateTime,Status)VALUES('" & profile.lblemployeenum.Text & "','" & profile.lblemployeename.Text & "','" & Main.lbldate.Text & "','" & logdate2 & "','" & logdate & "','" & status & "' )"
                Savelog(SQLStatement)

                srchTextBox.Clear()
                con.Close()

            Else
                'error
                Dim Errortext As New Msgerror
                Errortext.PassedText = "Record is not Updated"
                Errortext.ShowDialog()



            End If


        End If
    Else
        Dim Errortext As New Msgerror
        Errortext.PassedText = "All Entries with * must be filled"
        Errortext.ShowDialog()

    End If
End Sub


Solution 1:[1]

Firstly, create an instance of your form2 in form1.

Dim secondform As New Form2

On your form2, go to the 'modifiers' property of your three labels and change it to public.

Then you can set variables in form1 to get the value of the labels like the following;

Dim a As String = secondform.lblemployeename.Text
Dim b As String = secondform.lblemployeenum.Text
Dim c As String = secondform.lblEmpPosition.Text

This should make 'a', 'b' and 'c' the value of your labels

Solution 2:[2]

Just add the controls from the form's name:

Form2.lblemployeename.Text
Form2.lblemployeenum.Text
Form2.lblEmpPosition.Text

For me, in VB.NET 2.0 with VS2005, the controls are created Friend. Check the form designer and put your controls on Public or Friend (and With Events if you need it) in order to use them from other forms.

Solution 3:[3]

Looks like you are creating an instance of your form (Dim profile As New ProfileControl) and then you try to access his controls values. Dont you have a ProfileControl already created?

Anyway, if ProfileControl is a form, the Load event will only execute when the form loads (when you show the form), so the values for the labels are not getting loaded.

If you are showing the form already, dont create that instance and use the form name that you showed to access the labels.

If you are not showing the form, you should consider creating that values in another variables and later assign it to the labels or create your own class, etc.

Solution 4:[4]

A know this is a bit late, but I spended 2days to figure it out(I readed more then 100 pages from Google, stackoverflow, VBforums,..). Here si solution:

Create constants.vb (class) and put in code below:

Public Shared Property loading_window_status As String = ""

Now you can access loading_window_status from form1, form2.ShowDialog(), form3.Show().

form1:

constants.loading_window_status = "Test"

form2:

dim a as string
a = constants.loading_window_status

For me, nothing else is working, when I want to close loading_screen.ShowDialog() from loading_screen timer1.tick, but triger for closing is on form1.

Example:

Public Class form1
    Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'set status to show
        Constants.loading_window_status = "Show"
        'multitreads for spinner to run on loading_screen
        Application.DoEvents()
        'use ShowDialog or System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf form_loader_window.ShowDialog), Me)
        loading_screen.ShowDialog()

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs)
        'close screen loader
        Constants.loading_window_status = "Close"
    End Sub
End Class
Public Class loading_screen

    Private Sub loading_screen(sender As Object, e As EventArgs) Handles MyBase.Load

        Timer1.Interval = 500
        Timer1.Start()

    End Sub


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim what_todo As String
        'get status from "form1"
        what_todo = Constants.loading_window_state

        If what_todo = "Close" Then
        'if status close, close me (ShowDialog)
            Me.Close()
            Timer1.Stop()

        End If

    End Sub
End Class

Tested on Microsoft Visual Studio Community 2019 Preview.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cubsoft
Solution 2
Solution 3
Solution 4 Tom Slapar