MDI 子窗体 C#

发布于 2024-12-10 19:42:28 字数 44 浏览 0 评论 0原文

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

How do you check for a close event for an MDI child form when clicking the "X" button and let the parent form know that it has closed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如梦亦如幻 2024-12-17 19:42:28

您可以简单地监听 MDI 中的 FormClosed 事件。

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

You can simply listen to the FormClosed event in the MDI.

var childForm = new ChildForm();
childForm.FormClosed += new FormClosedEventHandler(form_FormClosed);
childForm.Show();
情话难免假 2024-12-17 19:42:28

在表单 FormClosing 事件中,您可以执行以下操作

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

In the form FormClosing event you can do

TheMainForm form = (TheMainForm)this.MdiParent()
form.AlertMe( this );
拥抱影子 2024-12-17 19:42:28

从 mainForm 中将关闭事件附加到子窗体

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

没有检查代码,但应该不那么难

Attach a closed event to the childform from within the 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

红ご颜醉 2024-12-17 19:42:28

好吧,下面的代码显示了父窗体如何识别子窗体是否已关闭,并且还可以识别是否有任何新的子窗体添加到该父窗体中。

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文