来自加载表单的VB .Net Pass值

时间:2018-05-15 00:03:55

标签: vb.net

我目前用VB VS2012制作项目

我有2个表格, form1包含文本框和按钮,此按钮用于打开form2。 在form2中,我有1个按钮。我想在单击form2按钮时设置form1.textbot值,然后关闭form2。

form1 button to call form2

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form2.ShowDialog(Me)
End Sub


this is button form2
 Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
    Dim x As New form1
    x.textbox.Text = "tes"

End Sub

我尝试了ctype,直播,但它不起作用。顺便说一句,这个form1停靠在一个mainform上。我试图做同样的事情,但没有停靠的形式,它可以工作,但当它停靠时,它不起作用。

3 个答案:

答案 0 :(得分:0)

尝试使用默认实例。

Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
    Form1.textbox.Text = "tes"
    Close()
End Sub

你真的为文本框文本框命名了吗?描述性名称可能会有所帮助。

答案 1 :(得分:0)

您目前正在做的是使用行form1创建Dim x As New form1的新实例 - 这意味着您没有更新原始form1

由于您已将对form1的引用传递到Form2对话框,即.ShowDialog(Me),这样就可以了:

'form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form2.ShowDialog(Me)
End Sub

'Form2
Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
    CType(Me.Owner, form1).textbox.Text = "tes"
End Sub

答案 2 :(得分:0)

我找到了我的解决方案

表格1上的代码

Private t1 As String = String.Empty
Private t2As String = String.Empty

 Dim f As New form1()
    If f.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        t1 = f.t1.ToString
        t2 = f.t2.ToString
        textbox.Text = t1
        textbox2.Text = t2
        f.Close()
    End If

以及form2上的

Public t1 As String
Public t2 As String

Me.DialogResult = Windows.Forms.DialogResult.OK
    If Me.DialogResult = Windows.Forms.DialogResult.OK Then
        t1 = dgdriver.SelectedRows(0).Cells(1).Value.ToString
        t2 = dgdriver.SelectedRows(0).Cells(0).Value.ToString
        Me.Close()
    End If