目录
1.思路
2.实例
3.实现效果
思路
在子窗体类中声明委托与事件,然后在父窗体给子窗体的事件绑定方法,最后在子窗体中触发事件并传值给事件,这样在父窗体被绑定的方法就会被执行,子窗体的值也能被父窗体接收
实例
Child_Dialog.cs(子窗口)
public partial class Child_Dialog : Form
{
public delegate void TextChange(string Text,Color color);
public event TextChange TextChange_Event;
public Child_Dialog()
{
InitializeComponent();
}
private void Tigger_Click(object sender, EventArgs e)
{
TextChange_Event("已触发",Color.Red);
this.Close();
}
}
Father_Form.cs(父窗口)
public partial class Father_Form : Form
{
public Father_Form()
{
InitializeComponent();
}
private void Btn_Run_Click(object sender, EventArgs e)
{
Child_Dialog child_Dialog = new Child_Dialog();
child_Dialog.TextChange_Event += Text_Change;
child_Dialog.Show();
}
private void Text_Change(string text,Color color)
{
label1.Text = text;
label1.BackColor = color;
}
}