<p><span style="font-size: 10pt;">本次示例效果如下:<br>form1为父窗体(包含textbox1、button1)<br>form2为子窗体(包含textbox2、button2)</span></p><p><span style="font-size: 10pt;">父窗体给子窗体传值<br>==================<br>1.点击form1的button1 打开form2<br> 父窗体给子窗体传值 可以调用重载子窗体的构造函数 直接传入相关数值</span></p><p><span style="font-size: 10pt;"> public partial class form1 : form<br> {<br> public form1()<br> {<br> initializecomponent();<br> }</span></p><p><span style="font-size: 10pt;"> private void button1_click(object sender, eventargs e)<br> {<br> form2 frm2 = new form2(this.textbox1.text);<br> frm2.show();<br> }<br> }</span></p><p><span style="font-size: 10pt;"> public partial class form2 : form<br> {<br> public form2()<br> {<br> initializecomponent();<br> }</span></p><p><span style="font-size: 10pt;"> public form2(string strtextbox1text)<br> {<br> initializecomponent();<br> this.textbox2.text = strtextbox1text;<br> }<br> }</span></p><p><span style="font-size: 10pt;">2.点击form1的button1 打开form2<br> 并调用子窗体form2的公开属性或方法 将form1的textbox1的值设置给form2的textbox2<br><br> public partial class form1 : form<br> {<br> public form1()<br> {<br> initializecomponent();<br> }</span></p><p><span style="font-size: 10pt;"> private void button1_click(object sender, eventargs e)<br> {<br> form2 frm2 = new form2();<br> frm2.textbox2text = this.textbox1.text;<br> frm2.show();<br> }<br> }</span></p><p><span style="font-size: 10pt;"> public partial class form2 : form<br> {<br> public form2()<br> {<br> initializecomponent();<br> }</span></p><p><span style="font-size: 10pt;"> public string textbox2text<br> {<br> set { this.textbox2.text = value; }<br> get { return this.textbox2.text; }<br> } <br> }<br><br>3.点击form1的button1 打开form2<br> 在form2_load调用父窗体form1的公开属性或方法 将form1的textbox1的值设置给form2的textbox2</span></p><p><span style="font-size: 10pt;"> public partial class form1 : form<br> {<br> public form1()<br> {<br> initializecomponent();<br> }</span></p><p><span style="font-size: 10pt;"> public string textbox1text<br> {<br> set { this.textbox1.text = value; }<br> get { return this.textbox1.text; }<br> }</span></p><p><span style="font-size: 10pt;"> private void button1_click(object sender, eventargs e)<br> {<br> form2 frm2 = new form2();<br> frm2.show(this);//或 frm2.showdialog(this);</span></p><p><span style="font-size: 10pt;"> 或者<br> //form2 frm2 = new form2();<br> //frm2.owner = this;<br> //frm2.show();//或 frm2.showdialog();<br> }<br> }<br> public partial class form2 : form<br> {<br> public form2()<br> {<br> initializecomponent();<br> }</span></p><p><span style="font-size: 10pt;"> private void form2_load(object sender, eventargs e)<br> {<br> form1 frm1 = (form1)this.owner;<br> this.textbox2.text = frm1.textbox1text;<br> }<br> }</span></p><p><span style="font-size: 10pt;">子窗体给父窗体传值<br>==================<br>4.点击form1的button1 打开form2<br> 再点击form2的button2 <br> 在button2_click事件中 通过this.owner将form2的textbox2的值设置给form1的textbox1<br> 并关闭form2</span></p><p><span style="font-size: 10pt;"> public partial class form1 : form<br> {<br> public form1()<br> {<br> initializecomponent();<br> }</span></p><p><span style="font-size: 10pt;"> private void button1_click(object sender, eventargs e)<br> {<br> form2 frm2 = new form2();<br> frm2.show(this);//或 frm2.showdialog(this);</span></p><p><span style="font-size: 10pt;"> 或者<br> //form2 frm2 = new form2();<br> //frm2.owner = this;<br> //frm2.show();//或 frm2.showdialog();<br> }<br> }</span></p><p><span style="font-size: 10pt;"> public partial class form2 : form<br> {<br> public form2()<br> {<br> initializecomponent();<br> }</span></p><p><span style="font-size: 10pt;"> private void button2_click(object sender, eventargs e)<br> {<br> form1 frm1 = (form1)this.owner;<br> //注意 如果textbox1是放在panel1中的 则先找panel1 再找textbox1<br> ((textbox)frm1.controls["textbox1"]).text = this.textbox2.text;<br> this.close();<br> }<br> }</span></p><p><span style="font-size: 10pt;">5.点击form1的button1 打开form2<br> 再点击form2的button2 <br> 在button2_click事件中 通过this.owner及调用父窗体form1的公开属性或方法 <br> 将form2的textbox2的值设置给form1的textbox1<br> 并关闭form2<br><br> public partial class form1 : form<br> {<br> public form1()<br> {<br> initializecomponent();<br> }</span></p><p><span style="font-size: 10pt;"> public string textbox1text<br> {<br> set { this.textbox1.text = value; }<br> get { return this.textbox1.text; }<br> }</span></p><p><span style="font-size: 10pt;"> private void button1_click(object sender, eventargs e)<br> {<br> form2 frm2 = new form2();<br> frm2.show(this);//或 frm2.showdialog(this);</span></p><p><span style="font-size: 10pt;"> 或者<br> //form2 frm2 = new form2();<br> //frm2.owner = this;<br> //frm2.show();//或 frm2.showdialog();<br> }<br> }</span></p><p><span style="font-size: 10pt;"> public partial class form2 : form<br> {<br> public form2()<br> {<br> initializecomponent();<br> }</span></p><p><span style="font-size: 10pt;"> private void button2_click(object sender, eventargs e)<br> {<br> form1 frm1 = (form1)this.owner;<br> frm1.textbox1text = this.textbox2.text;<br> this.close();<br> }<br> }<br></span></p>
c# winform 父窗体 子窗体 传值
最新推荐文章于 2021-12-31 15:49:25 发布