
方法一: A to B
设置FormB 为 带参数的构造函数
public Form2( object msg)
{
InitializeComponent();
}
方法二: A to B
定义一个public 函数
public void Receive(string Msg)
{
txtInfo.Text = Msg;
this.Show();
}
方法三: 两个窗口间相互传值
需要互传时 方法二可以实现 参数传递 但是不能及时更新接收窗口控件的显示,所以建议使用委托的方法。
Form1:
private void btnShow_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.MyEvent += new Form2.MyDelegate(MyEvent);
f2.Receive(txtInfo1.Text);
}
void MyEvent(string msg)
{
txtInfo1.Text = msg;
}
Form2:
public delegate void MyDelegate(string msg);
public event MyDelegate MyEvent; private void btnSend_Click(object sender, EventArgs e)
{
MyEvent(txtInfo.Text);
}