简体   繁体   English

MDI子表格C#

[英]MDI Child Forms C#

单击“ X”按钮时如何检查MDI子窗体的关闭事件,并让父窗体知道它已关闭?

You can simply listen to the FormClosed event in the MDI. 您可以简单地听MDI中的FormClosed事件。

var childForm = new ChildForm();
childForm.FormClosed += new FormClosedEventHandler(form_FormClosed);
childForm.Show();

In the form FormClosing event you can do 在FormClosing事件中,您可以执行

TheMainForm form = (TheMainForm)this.MdiParent()
form.AlertMe( this );

Attach a closed event to the childform from within the mainForm 从mainForm内将关闭事件附加到子窗体

Form mdiChild = new Form();
mdiChild.MdiParent = this;
mdiChild.Closed += (s, e) => { //... };
mdiChild.Show();

didn't check the code but should not be that hard 没有检查代码,但不应该那么难

well, The below code shows how parent form recognises whether the child form has been closed or not and it can also recognises that is there any new child form is added to that parent form.. 很好,下面的代码显示了父窗体如何识别子窗体是否已关闭,还可以识别出是否有任何新的子窗体添加到该父窗体。

private List<Form> ChildFormList = new List<Form>();

private void MyForm_MdiChildActivate(object sender, EventArgs e)
{
   Form f = this.ActiveMdiChild;

   if (f == null)
   {
    //the last child form was just closed
    return;
   }

   if (!ChildFormList.Contains(f))
   {
      //a new child form was created
      ChildFormList.Add(f);
      f.FormClosed += new FormClosedEventHandler(ChildFormClosed); // here the parent form knows that that child form has been closed or not
  }
  else
  {
    //activated existing form
  }
}
private void ChildFormClosed(object sender, FormClosedEventArgs e)
{
   //a child form was closed
    Form f = (Form)sender;
    ChildFormList.Remove(f);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM