WinForm窗口之间传递参数的几种方法。(下面做了一个小示例,因其浅显易懂,就不加注释了。@) using System; using ; using ponentModel; using ; using ; using ; using ; using ; namespace WinPassValue { public partial class MainForm : Form { public MainForm() { ponent(); SelectedType(); } public delegate void ChangeFormValue(string s1); //开始传递 private void btnPass_Click(object sender, EventArgs e) { if ( == 3) { Form3 f3 = new Form3(txtValue); (); } else if ( == 2) { Form2 f2 = new Form2(); (); } else if ( == 1) { = ; Form1 f1 = new Form1(); (); } //利用Show方法重载实现。注意,此时要设置要传递控件访问权限为public 或 internal else if ( == 5) { Form5 f5 = new Form5(); (this); } //委托事件传值 else if ( == 4) { Form4 f4 = new Form4(); //订阅事件 += new (myPassValue); (); } } //定义方法 public string myPassValue() { return ();
}
//定义方法用于选择传递参数的方式 private void SelectedType() { string[] strValue = { "-请选择要传递的方式-","全局变量传递","构造函数传值","控件传值","委托事件传值", "Show方法重载!"}; (strValue); = 0; } private void MainForm_Load(object sender, EventArgs e) { } } } using System; using ; using ponentModel; using ; using ; using ; using ; using ; namespace WinPassValue { public partial class Form1 : Form { public Form1() { ponent(); } private void Form1_Load(object sender, EventArgs e) { = true; = +"采用全局变量的 方式实现!"; } } } using System; using