使用 vb.net 从其他形式获取标签的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14939942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:23:14  来源:igfitidea点击:

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

vb.net

提问by Harvey

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

实际上我在这里使用 mysql 作为我的数据库

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.

这是将检索 3 个标签的值的表单。

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

回答by Cubsoft

Firstly, create an instance of your form2 in form1.

首先,在form1 中创建一个form2 的实例。

Dim secondform As New Form2

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

在您的 form2 上,转到三个标签的 'modifiers' 属性并将其更改为 public。

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

然后你可以在form1中设置变量来获取标签的值,如下所示;

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

这应该使 'a'、'b' 和 'c' 成为标签的值

回答by SysDragon

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 ProfileControlalready created?

看起来您正在创建表单 ( Dim profile As New ProfileControl) 的一个实例,然后您尝试访问他的控件值。你不是ProfileControl已经创建了吗?

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

无论如何,如果ProfileControl是表单,则Load事件只会在表单加载时(当您显示表单时)执行,因此标签的值不会被加载。

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.

如果您不显示表单,则应考虑在另一个变量中创建该值,然后将其分配给标签或创建您自己的类等。

回答by SysDragon

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 Publicor Friend(and With Eventsif you need it) in order to use them from other forms.

对我来说,在带有 VS2005 的 VB.NET 2.0 中,控件是创建的Friend。检查表单设计器并将您的控件放在PublicFriendWith Events如果您需要的话)上,以便从其他表单中使用它们。