做个笔记,防止忘记
运用委托,子窗口向父窗口传值,效果如下
子窗口代码namespace WinForm
{
//申明一个委托
public delegate void TransfDelegate(String value);
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public event TransfDelegate TransfEvent;
private void button1_Click(object sender, EventArgs e)
{
//触发这个委托
TransfEvent(textBox1.Text);
//关闭子窗体
this.Close();
}
}
}
父窗口代码namespace WinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//事件处理方法
void frm_TransfEvent(string value)
{
textBox1.Text = value;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
//注册事件
frm.TransfEvent += frm_TransfEvent;
frm.ShowDialog();
}
}
}