您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > C/C++资料 > C# WinForm 父窗体 子窗体 传值
C#WinForm父窗体子窗体传值本次示例效果如下:Form1为父窗体(包含textBox1、button1)Form2为子窗体(包含textBox2、button2)父窗体给子窗体传值==================1.点击Form1的button1打开Form2父窗体给子窗体传值可以调用重载子窗体的构造函数直接传入相关数值publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){Form2frm2=newForm2(this.textBox1.Text);frm2.Show();}}publicpartialclassForm2:Form{publicForm2(){InitializeComponent();}publicForm2(stringstrTextBox1Text){InitializeComponent();this.textBox2.Text=strTextBox1Text;}}2.点击Form1的button1打开Form2并调用子窗体Form2的公开属性或方法将Form1的textBox1的值设置给Form2的textBox2publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){Form2frm2=newForm2();frm2.TextBox2Text=this.textBox1.Text;frm2.Show();}}publicpartialclassForm2:Form{publicForm2(){InitializeComponent();}publicstringTextBox2Text{set{this.textBox2.Text=value;}get{returnthis.textBox2.Text;}}}3.点击Form1的button1打开Form2在Form2_Load调用父窗体Form1的公开属性或方法将Form1的textBox1的值设置给Form2的textBox2publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}publicstringTextBox1Text{set{this.textBox1.Text=value;}get{returnthis.textBox1.Text;}}privatevoidbutton1_Click(objectsender,EventArgse){Form2frm2=newForm2();frm2.Show(this);//或frm2.ShowDialog(this);////或者//Form2frm2=newForm2();//frm2.Owner=this;//frm2.Show();//或frm2.ShowDialog();}}publicpartialclassForm2:Form{publicForm2(){InitializeComponent();}privatevoidForm2_Load(objectsender,EventArgse){Form1frm1=(Form1)this.Owner;this.textBox2.Text=frm1.TextBox1Text;}}子窗体给父窗体传值==================4.点击Form1的button1打开Form2再点击Form2的button2在button2_Click事件中通过this.Owner将Form2的textBox2的值设置给Form1的textBox1并关闭Form2publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){Form2frm2=newForm2();frm2.Show(this);//或frm2.ShowDialog(this);////或者//Form2frm2=newForm2();//frm2.Owner=this;//frm2.Show();//或frm2.ShowDialog();}}publicpartialclassForm2:Form{publicForm2(){InitializeComponent();}privatevoidbutton2_Click(objectsender,EventArgse){Form1frm1=(Form1)this.Owner;//注意如果textBox1是放在panel1中的则先找panel1再找textBox1((TextBox)frm1.Controls[textBox1]).Text=this.textBox2.Text;this.Close();}}5.点击Form1的button1打开Form2再点击Form2的button2在button2_Click事件中通过this.Owner及调用父窗体Form1的公开属性或方法将Form2的textBox2的值设置给Form1的textBox1并关闭Form2publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}publicstringTextBox1Text{set{this.textBox1.Text=value;}get{returnthis.textBox1.Text;}}privatevoidbutton1_Click(objectsender,EventArgse){Form2frm2=newForm2();frm2.Show(this);//或frm2.ShowDialog(this);////或者//Form2frm2=newForm2();//frm2.Owner=this;//frm2.Show();//或frm2.ShowDialog();}}publicpartialclassForm2:Form{publicForm2(){InitializeComponent();}privatevoidbutton2_Click(objectsender,EventArgse){Form1frm1=(Form1)this.Owner;frm1.TextBox1Text=this.textBox2.Text;this.Close();}}
本文标题:C# WinForm 父窗体 子窗体 传值
链接地址:https://www.777doc.com/doc-7029066 .html