C#实现窗体间传递数据实例
程序员文章站
2023-12-18 16:20:10
本文以实例详述了c#两个窗体之间传递数据的实现方法,具体的操作步骤如下:
1.建立两个窗体,并采用公用变量值传递:
public partial class f...
本文以实例详述了c#两个窗体之间传递数据的实现方法,具体的操作步骤如下:
1.建立两个窗体,并采用公用变量值传递:
public partial class form1 : form //父窗体 { public string name=""; public form1() { initializecomponent(); } private void newbtn_click(object sender, eventargs e) { form2 form2 =new form2(); form2.showdialog(); if (form2.dialogresult == dialogresult.ok) { textbox1.text = form2.name; form2.close(); } } }
public partial class form2 : form // 子窗体 { public string name { set { textbox1.text = value; } get { return textbox1.text; } } public form2() { initializecomponent(); } private void ok_click(object sender, eventargs e) { if (textbox1.text == "") { messagebox.show("input!"); return; } dialogresult = dialogresult.ok; close(); } }
2.使用地址方式传递
public partial class form1 : form //parent form { public string name=""; public form1() { initializecomponent(); } private void newbtn_click(object sender, eventargs e) { form2 form2 =new form2(); form2.owner = this;//form2的指针指向form1 form2.showdialog(); textbox1.text = form2.name; form2.close(); } }
public partial class form2 : form //son form { public string name { set { textbox1.text = value; } get { return textbox1.text; } } public form2() { initializecomponent(); } private void ok_click(object sender, eventargs e) { if (textbox1.text == "") { messagebox.show("input!"); return; } form1 form1 = (form1)this.owner;//form2的父窗体指针赋给form1 close(); } }