跨窗体控制、传值

将父窗体控件上的值传入子窗体的控件上:

Form1为父窗体

Form2为子窗体

Form1 单击按钮打开Form2,Form2关闭的时候把值传到Form1的控件上。

 

Form1代码:

 

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this.textBox1.Text);
if (f2.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = f2.TextBoxValue;
f2.Close();
}

}

 

Form2代码:

public Form2():this(null)
{

}

public string TextBoxValue
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}

public Form2(string value) {
InitializeComponent();
TextBoxValue = value;
}

private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}

}

 

posted on 2017-07-13 16:34  homie  阅读(102)  评论(0编辑  收藏  举报