2

它不起作用,标签只会恢复为默认值。你认为是什么问题?

好的,这是我的代码:

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

这是生成标签值的形式:

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

这是将检索 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
4

4 回答 4

1

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

Dim secondform As New Form2

在您的 form2 上,转到三个标签的“修饰符”属性并将其更改为公共。

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

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

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

于 2013-02-18T15:43:03.833 回答
0

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

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

如果您已经在显示表单,请不要创建该实例并使用您显示的表单名称来访问标签。

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

于 2013-02-18T16:54:06.400 回答
0

只需添加表单名称中的控件:

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

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

于 2013-02-18T15:42:12.903 回答
0

知道这有点晚了,但我花了 2 天时间才弄清楚(我从 Google、stackoverflow、VBforums 阅读了 100 多页……)。这里是解决方案:

创建constants.vb(类)并输入以下代码:

Public Shared Property loading_window_status As String = ""

现在您可以从 form1、form2.ShowDialog()、form3.Show() 访问 loading_window_status。

表格1:

constants.loading_window_status = "Test"

表格2:

dim a as string
a = constants.loading_window_status

对我来说,当我想从 loading_screen timer1.tick 关闭 loading_screen.ShowDialog() 时,没有其他工作,但关闭的触发器在 form1 上。

例子:

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

在 Microsoft Visual Studio Community 2019 Preview 上测试。

于 2022-02-03T14:46:01.337 回答