WinForm窗口之间传递参数的几种方法。(下面做了一个小示例,因其浅显易懂,就不加注释了。若有疑问可给我发邮件yaotou.cn@qq.com)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinPassValue
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
SelectedType();
}
public delegate void ChangeFormValue(string s1);
//开始传递
private void btnPass_Click(object sender, EventArgs e)
{
if (cbmSelectedType.SelectedIndex == 3)
{
Form3 f3 = new Form3(txtValue);
f3.Show();
}
else if (cbmSelectedType.SelectedIndex == 2)
{
Form2 f2 = new Form2(txtValue.Text);
或 internal f2.Show();
}
else if (cbmSelectedType.SelectedIndex == 1)
{
Program.yourName = txtValue.Text;
Form1 f1 = new Form1();
f1.Show();
}
//利用Show方法重载实现。注意,此时要设置要传递控件访问权限为public else if (cbmSelectedType.SelectedIndex == 5)
{
Form5 f5 = new Form5();
f5.Show(this);
}
//委托事件传值
else if (cbmSelectedType.SelectedIndex == 4)
{
Form4 f4 = new Form4();
//订阅事件
f4.MyEvent += new Form4.PassValue(myPassValue);
f4.Show();
}
}
//定义方法
public string myPassValue()
{
return txtValue.Text.Trim();
}
//定义方法 用于选择传递参数的方式
private void SelectedType()
{
string[] strValue = { \"-请选择要传递的方式-\",\"全局变量传递\",\"构造函数传值\",\"控件传值\",\"委托事件传值\",
\"Show方法重载!\"};
cbmSelectedType.Items.AddRange(strValue);
cbmSelectedType.SelectedIndex = 0;
}
private void MainForm_Load(object sender, EventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinPassValue
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.ReadOnly = true;
textBox1.Text = Program.yourName+\"采用全局变量的方式实现!\";
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinPassValue
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private string s2;
public Form2( string s1)
{
InitializeComponent();
s2= s1;
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.ReadOnly = true;
textBox1.Text = \"“\" + s2 + \"”\" + \"采用构造函数实现!\";
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinPassValue
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private string ss;
public Form3(TextBox te)
{
InitializeComponent();
ss = te.Text; ;
}
private void Form3_Load(object sender, EventArgs e)
{
textBox1.ReadOnly = true;
textBox1.Text = \"“\" + ss + \"”\" + \"采用传递控件的方式实现!\";
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinPassValue
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
//定义委托
public delegate string PassValue();
//定义事件
public event PassValue MyEvent;
//委托事件传值
private void Form4_Load(object sender, EventArgs e)
{
textBox1.ReadOnly = true;
textBox1.Text=MyEvent()+\"委托事件传值实现!\";
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinPassValue
{
public partial class Form5 : Form
{
public Form5()
现!\";
{
InitializeComponent();
}
//Show方法重载!
private void Form5_Load(object sender, EventArgs e)
{
MainForm mf=(MainForm)this.Owner;
textBox1.Text = ((TextBox)mf.txtValue).Text+\"采用Show方法重载方式实
}
}
}
当然还有其他的方法,欢迎大家互相交流学习!