1.子窗体事件刷新父窗体界面值
子窗体定义委托和事件
//声明一个委托 public delegate void DisplayUpdateDelegate(string str); //声明事件 public event DisplayUpdateDelegate ShowUpdate; private void Form2_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if (ShowUpdate != null) { ShowUpdate(textBox1.Text.ToString()); } }
父窗体
private void button3_Click(object sender, EventArgs e) { Form2 _frm2 = new Form2(); _frm2.ShowUpdate += new Form2.DisplayUpdateDelegate(ShowMessage); _frm2.Show(); } private void ShowMessage(string str) { textBox1.Text = str; }
2.父窗体的事件刷新子窗体的值
父窗体代码
public delegate void ChangeSonWindowDelegate(string str); ChangeSonWindowDelegate _changeSonWindow; private void button1_Click(object sender, EventArgs e) { Form2 _frm2 = new Form2(); _changeSonWindow = new ChangeSonWindowDelegate(_frm2.ShowMessage); _frm2.Show(); } private void button2_Click(object sender, EventArgs e) { _changeSonWindow(textBox1.Text); }
子窗体代码
public void ShowMessage(string str) { textBox1.Text = str; }
时间: 2024-10-23 06:57:11