功能说明:母窗(Form1)体点击按钮,打开子窗体(Form2),将子窗体的文本框 textBox2 的值传入到母窗体 textBox1 中。
1:Form1.cs 中:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.ShowDialog();
}
//因为textBox1是Private的,所以这里要提供外部访问的属性接口
//如果没有这一段,就必须在Form1.Designer.cs中把textBox1的属性设置为public
public string TextBox1
{
set { this.textBox1.Text = value; }
get { return this.textBox1.Text; }
}
2:Form2.cs中:
Form _parentForm = new Form();
//自定义一个构造方法来接手母窗体
public Form4(Form parentForm)
{
InitializeComponent();
this._parentForm = parentForm;
}
private void button1_Click(object sender, EventArgs e)
{
((Form1)_parentForm).TextBox1 = this.textBox1.Text; //调用父窗体控件
this.Dispose();
}