|
C#简单实现子窗体向父窗体传值的方法
点击Form1的button1 打开Form2
再点击Form2的button2
在button2_Click事件中 通过this.Owner及调用父窗体Form1的公开属性或方法
将Form2的textBox2的值设置给Form1的textBox1
并关闭Form2
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public string TextBox1Text
- {
- set { this.textBox1.Text = value; }
- get { return this.textBox1.Text; }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Form2 frm2 = new Form2();
- frm2.Show(this);//或 frm2.ShowDialog(this);
- ////或者
- //Form2 frm2 = new Form2();
- //frm2.Owner = this;
- //frm2.Show();//或 frm2.ShowDialog();
- }
- }
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Form1 frm1 = (Form1)this.Owner;
- frm1.TextBox1Text = this.textBox2.Text;
- this.Close();
- }
- }
复制代码
|
|