C# MDI Parent 检测 MDI Child 何时关闭?

发布于 2024-10-06 03:31:59 字数 889 浏览 0 评论 0原文

我试图在 MDI 父窗体上检测我的 MDI 子窗体何时关闭,并做出相应的反应。在 MDI 子级关闭之前,MDI 父级不应执行任何操作。这是我的代码,我不确定我做错了什么,但是我添加的表单关闭事件方法永远不会被调用...

以下代码位于 MDI 父类中,如果这不是很明显的话。

    private void keyValidation()
    {
        if (Properties.Settings.Default.Unlock == true)
            return;
        else
        {
            menu.Enabled = false;
            statusStrip.Enabled = false;

            ValidationForm vf = new ValidationForm();
            vf.MdiParent = this;
            vf.Show();
            vf.FormClosed += new FormClosedEventHandler(validationForm_FormClosed);
        }
    }

    void validationForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        MessageBox.Show("Got here");
        if (Properties.Settings.Default.Unlock == true)
        {
            menu.Enabled = true;
            statusStrip.Enabled = true;
        }
    }

感谢您的帮助!

I'm attempting to detect, on the MDI parent, when my MDI child form closes, and react accordingly. The MDI parent shouldn't do anything until the MDI child closes. Here is my code, I'm unsure as to what I'm doing wrong, but the form closed event method I added is never being called...

The following code is in the MDI parent class, if that wasn't obvious.

    private void keyValidation()
    {
        if (Properties.Settings.Default.Unlock == true)
            return;
        else
        {
            menu.Enabled = false;
            statusStrip.Enabled = false;

            ValidationForm vf = new ValidationForm();
            vf.MdiParent = this;
            vf.Show();
            vf.FormClosed += new FormClosedEventHandler(validationForm_FormClosed);
        }
    }

    void validationForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        MessageBox.Show("Got here");
        if (Properties.Settings.Default.Unlock == true)
        {
            menu.Enabled = true;
            statusStrip.Enabled = true;
        }
    }

Thanks for any help!

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

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

发布评论

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

评论(6

怂人 2024-10-13 03:32:00

也许,只是也许,您有一个隐藏而不是关闭的子表单。尝试挂接 VisibleChanged 并看看会发生什么。

此外,FormClosing 将允许您取消关闭,而 FormClosed 不会为您提供该选项。

Maybe, just maybe, you have a CHILD form that HIDES not CLOSES. Try hooking VisibleChanged and see what happens.

Also, FormClosing will allow you to cancel closing, and FormClosed doesn't give you that option.

黯然#的苍凉 2024-10-13 03:32:00

不要使用 Closed 事件。相反,请使用 FormClosing 事件:

private void frmMain_FormClosing(object sender, FormClosingEventArgs e){
  if (MessageBox.Show("Are you sure you want to Exit", "Confirmation", MessageBoxButtons.YesNo,MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.No) {
    e.Cancel = true;
  }
}

Don't use the Closed Event. Instead, use the FormClosing event:

private void frmMain_FormClosing(object sender, FormClosingEventArgs e){
  if (MessageBox.Show("Are you sure you want to Exit", "Confirmation", MessageBoxButtons.YesNo,MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.No) {
    e.Cancel = true;
  }
}
入画浅相思 2024-10-13 03:32:00

当通过将 IsMdiContainer 设置为 true 将 ParentForm 标记为 MdiContainer 时,将引发 ParentForm.ControlAdded 事件以将“MdiClient”控件添加到父窗体。因此,当将 MdiClient 添加到父 MDI 表单时,我们可以为 MdiClient 控件引发 ControlAdded 事件,如下所示,

  public ParentForm()
  {
    InitializeComponent();
    this.ControlAdded += Form1_ControlAdded;
    this.IsMdiContainer = true;

我们需要如下引发 MdiClient.ControlAdded ,

    void Form1_ControlAdded(object sender, ControlEventArgs e)
      {
           if(e.Control is MdiClient)
                e.Control.ControlAdded += MdiClient_ControlAdded;

      }

默认情况下 MDI子窗体被添加到父窗体中 MdiClient 的控件集合中。因此,当将 ChildForm.MdiParent 值设置为父窗体时,将引发 MdiClient 的 ControlAdded 事件

void MdiClient_ControlAdded(object sender, ControlEventArgs e)
{
    if (e.Control is Form)
        {
            var form = e.Control as Form;
            form.FormClosing += form_FormClosing;
            form.FormClosed += form_FormClosed;
        }
}

在上面的 MdiClient_ControlAdded 方法中,当子表单添加到父 MDI 表单中时,会引发该方法。因此,通过引发子表单的 FormClosing 和 FormClosed 事件,您可以轻松检测子表单是否关闭。

When mark the ParentForm as MdiContainer by setting the IsMdiContainer to true, the ParentForm.ControlAdded event raised for adding the "MdiClient" control to the parent form. So when adding MdiClient to parent MDI form, we can raise the ControlAdded event for the MdiClient control as like below,

  public ParentForm()
  {
    InitializeComponent();
    this.ControlAdded += Form1_ControlAdded;
    this.IsMdiContainer = true;

We need to raise the MdiClient.ControlAdded as like the below,

    void Form1_ControlAdded(object sender, ControlEventArgs e)
      {
           if(e.Control is MdiClient)
                e.Control.ControlAdded += MdiClient_ControlAdded;

      }

By default the MDI Child forms are added into the controls collection of the MdiClient in Parent form. So when set the ChildForm.MdiParent value as Parent form, the ControlAdded event for the MdiClient will raise.

void MdiClient_ControlAdded(object sender, ControlEventArgs e)
{
    if (e.Control is Form)
        {
            var form = e.Control as Form;
            form.FormClosing += form_FormClosing;
            form.FormClosed += form_FormClosed;
        }
}

In this above MdiClient_ControlAdded method raises when child form added into the Parent MDI form. So by raising the FormClosing and FormClosed events for child forms you can easily detect whether the child form is closed or not.

梦过后 2024-10-13 03:32:00

当您只需调用 MDI Parent 到 MDI Child 的事件处理程序时,为什么要做这么多事情?

假设我想要在我的孩子被激活或停用时做某些事情。您只需要声明 MDI Parent 的一些事件处理程序(例如 SetupToolStripMenu_PlantMasterRecipe)并提及您想要执行的操作中的一些逻辑。通过这种方式,您可以从 MDI Parent 控制 MDI Child 的每个对象行为。

这里 objB 是我的子窗体,我在 MDI 父窗体中编写以下代码:

objB.MdiParent = this;

objB.Activated += SetupToolStripMenu_PlantMasterRecipe;

objB.Deactivate += DisposeToolStripMenu;

objB.Show();

Why you are doing so many things when you can just invoke an event handler of MDI Parent to MDI Child?

Suppose I want to do certain things when my child will be activated or deactivated. You just need to declare some event handlers of MDI Parent (as e.g. SetupToolStripMenu_PlantMasterRecipe) and mention some logic inside that what you want to do. By this way, you can control each object behaviour of MDI Child from MDI Parent.

Here objB is my Child form and I am writing this code inside MDI Parent:

objB.MdiParent = this;

objB.Activated += SetupToolStripMenu_PlantMasterRecipe;

objB.Deactivate += DisposeToolStripMenu;

objB.Show();
嘿嘿嘿 2024-10-13 03:32:00

在 mdiparent 上创建一个公共函数

public void MakeMenuVisible()
{
MainMenu.visible = true;
}

然后在子窗体上您可以像这样运行该函数

private void ChildForm_FormClosed(object sender, FormClosedEventArgs e)
{
//Cast MdiParent to Mainform
((mainform)this.MdiParent).MakeMenuVisible();  
}

On mdiparent make a public function

public void MakeMenuVisible()
{
MainMenu.visible = true;
}

Then on childform you can run the function like this

private void ChildForm_FormClosed(object sender, FormClosedEventArgs e)
{
//Cast MdiParent to Mainform
((mainform)this.MdiParent).MakeMenuVisible();  
}
私野 2024-10-13 03:31:59

虽然这并不能真正解决您所指的问题,但从用例来看,您可能需要考虑将验证表单作为模式对话框而不是作为 MDI 子项打开。

您可以使用表单的 ShowDialog()Show() 的方法。请记住,ShowDialog() 还可以返回 DialogResult 如果您将它们分配给其他表单上的按钮。

While this doesn't really address the problem you're referring to, judging from the use case, you may want to consider opening the Validation form as a modal dialog instead of as an MDI child.

You can do this using the form's ShowDialog() method where you have Show() now. Keep in mind that ShowDialog() can also return a DialogResult if you assign them to buttons on the other form.

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