如何将数据从父窗体传递到子窗体

时间:2014-02-22 03:29:03

标签: vb.net

我有两个表单,Form1是父表单,Form2是子表单。它们都设置为同时显示..... Form1的中间容器设置为true并有一个按钮,Form2有一个文本框...我想要它,如果我按下Form1中的按钮的东西将出现在Form2的文本框中

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

    IsMdiContainer = True
    Dim inv As New Form2
    inv.MdiParent = Me
    inv.Show()
    inv.Location = New Point(15, 15)

End Sub

我尝试点击按钮但没有发生任何事情,我也尝试了另一种方式...在Form2中放置一个按钮,在Form1中放置一个文本框,它可以工作......

2 个答案:

答案 0 :(得分:0)

假设你在Form2上有一个TextBox1控件,在Form1上有一个按钮,当单击Form1上的按钮时,“Hello World”将出现在Form2上的TextBox1上,就这样做......

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Form2.TextBox1.Text="Hello World"
End Sub

答案 1 :(得分:0)

您必须将inv变量移出form_load范围

Public Class Form1

Private inv As New Form2 'here

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    IsMdiContainer = True

    inv.MdiParent = Me
    inv.Show()
    inv.Location = New Point(15, 15)
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    inv.TextBox1.Text = "Hello World"
End Sub

End Class