Form1为父窗体(包含textBox1、button1)
Form2为子窗体(包含textBox2、button2)
点击Form1的button1 打开Form2,把textbox1的值传给textbox2。再点击Form2的button2
在button2_Click事件中 通过this.Owner将Form2的textBox2的值设置给Form1的textBox1
并关闭Form2。
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.TextBox2Text = this.textBox1.Text;
frm2.Show(this);//或 frm2.ShowDialog(this);
}
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;
this.Close();
}
public string TextBox2Text
{
set { this.textBox2.Text = value; }
get { return this.textBox2.Text; }
}