C#简单实现子窗体向父窗体传值的方法

2019-12-26 13:21:55王振洲

将Form2的textBox2的值设置给Form1的textBox1

并关闭Form2

 

 
  1. public partial class Form1 : Form  { 
  2. public Form1()  { 
  3. InitializeComponent();  } 
  4. public string TextBox1Text  { 
  5. set { this.textBox1.Text = value; }  get { return this.textBox1.Text; } 
  6. }  private void button1_Click(object sender, EventArgs e) 
  7. {  Form2 frm2 = new Form2(); 
  8. frm2.Show(this);//或 frm2.ShowDialog(this);  ////或者 
  9. //Form2 frm2 = new Form2();  //frm2.Owner = this; 
  10. //frm2.Show();//或 frm2.ShowDialog();  } 
  11. }  public partial class Form2 : Form 
  12. {  public Form2() 
  13. {  InitializeComponent(); 
  14. }  private void button2_Click(object sender, EventArgs e) 
  15. {  Form1 frm1 = (Form1)this.Owner; 
  16. frm1.TextBox1Text = this.textBox2.Text;  this.Close(); 
  17. }  } 

希望本文所述对大家的C#程序设计有所帮助。