父窗体中有一个Label,子窗体中有一个TextBox和一个Button。在子窗体TextBox中输入文字,单击Button,父窗体Label中的文字改成同样文字。
父窗体:
namespace ParentAndChildTEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 fm2 = new Form2();
fm2.setValueEvent += new Form2.setValueEventHandler(setValue);
fm2.Show();
}
private void setValue(string str)
{
this.label1.Text = str;
}
}
}
子窗体:
namespace ParentAndChildTEST
{
public partial class Form2 : Form
{
public delegate void setValueEventHandler(string str);
public setValueEventHandler setValueEvent;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Invoke(setValueEvent, new object[] { this.textBox1.Text });
}
}
}