父窗口控制它打开的所有子窗口
1.问题描述:父窗体控制所有它打开的子窗体
2.方案:在父窗体声明一个事件,该事件在所有它他开的子窗体中实现。在主窗口每次出发该事件时就会将相关操作发送到子窗体了
-----------------------------------------------------
public partial class Form1 : Form
{
public delegate void ControlChildFormHander(string msg, string user, string ip);
/// <summary>
/// 父窗体控制子窗体事件
/// </summary>
public event ControlChildFormHander OnControlChildForm;
private void button1_Click(object sender, EventArgs e)
{
if (this.OnControlChildForm != null)
this.OnControlChildForm(DateTime.Now.ToString(), "", "");
}
}
public partial class DialogFrm : Form
{
private Form1 m_mainFrm;
public DialogFrm(Form1 mainFrm)
{
this.m_mainFrm = mainFrm;
}
private void DialogFrm_Load(object sender, EventArgs e)
{
this.m_mainFrm.OnControlChildForm += new Form1.ControlChildFormHander(m_mainFrm_OnControlChildForm);
}
void m_mainFrm_OnControlChildForm(string msg, string user, string ip)
{
······
}
}