如何防止 MDI 主窗体从 MDI 子窗体关闭

发布于 2024-08-31 22:02:45 字数 503 浏览 2 评论 0原文

我有一个 MDI 主窗体。我在上面托管了一个表单,我希望它在关闭之前显示一个消息框(询问用户是否保存更改)。

到目前为止一切顺利,但我发现关闭 MDI 主窗体不会引发 MDI 子 FormClosing 事件。我想我将在 MDI 主的 FormClosing 事件(确实会引发)中调用 MdiChild.Close() 。这似乎可以工作,但是它确实会导致问题...

在我显示的消息框中,我建议用户保存更改,而不是保存更改并取消关闭。通常这工作正常,但是我似乎找不到取消 MDI main 的 FormClosing 事件的方法。有没有一种优雅的方法来做到这一点?

编辑:我通过抛出异常(当用户决定取消关闭过程时)解决了这个问题,该异常在 MDI main 的 FormClosing 事件中捕获。通过这种方式,我知道何时必须取消 MDI 主程序的 FormClosing 事件,并且这看起来工作正常......但是我只是不敢相信这个“​​hax”是执行此操作的唯一方法。当然有更好的方法吗?

I have a MDI main form. On it I host a form, and I want it to show a message box before it closes (asking the user whether to save changes).

So far so good, however I have discovered that closing the MDI main form does not raise a MDI child FormClosing event. I figured I will just call MdiChild.Close() in the MDI main's FormClosing event (which does get raised). This seams to work, however it does causes a problem...

In the messagebox that I show, I offer the user to save changes, not to save changes and cancel closing. Normally this works fine, however I can't seem to find a way to cancel MDI main's FormClosing event. Is there a elegant way of doing this?

EDIT: I solved this by throwing an exception (when user decides to cancel the closing procedure) which is caught in MDI main's FormClosing event. In this way I know when I have to cancel the MDI main's FormClosing event and this seams to work fine ... However I just can't believe that this "hax" is the only way of doing this. Surely there is a better way?

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

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

发布评论

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

评论(3

冰雪之触 2024-09-07 22:02:45

我认为当用户投票取消关闭时,您会取消子表单上的关闭吗?

在这种情况下,我会关闭此主窗体

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        foreach (Form frm in this.MdiChildren)
        {
            frm.Close();
        }
    }
    e.Cancel = (this.MdiChildren.Length > 0);
}

I figure that you cancel the close on the childform when the user votes to cancel the close?

In that case I'd go with this Main form close

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        foreach (Form frm in this.MdiChildren)
        {
            frm.Close();
        }
    }
    e.Cancel = (this.MdiChildren.Length > 0);
}
浴红衣 2024-09-07 22:02:45

我建议您不要在子表单上调用 Close ,而是可以在子表单中创建一个类似 ReadyToClose 的方法,然后主表单循环并为每个表单调用该方法子表单,它会向用户询问问题,并在需要时进行保存,最后如果可以继续,它将返回 true。

如果所有的孩子都“投票”结束,那么你就将其全部关闭,否则你什么也不关闭。

I'd suggest that instead of calling Close on the childforms you could create a method like ReadyToClose in the child forms and then the main form loops through and calls that for each of the child forms and it'll ask the question to the user and do the saving if needed and finally it'll return true if ok to continue.

And if all of the child forms "vote" for a close, then you close it all down, otherwise you close nothing.

丢了幸福的猪 2024-09-07 22:02:45

关闭 MDI 主窗体时,首先调用所有子 Close 事件,因此,当在 foreach 循环上调用 frm.Close() 时,会再次调用子窗体的 Close 事件(我不知道为什么它们应该已经关闭) 。

ho1的建议对我来说非常有效。这是我修改后的 foreach 循环(ClosableMDIChildForm 是一个包含 IsOkToClose() 方法的接口):

foreach (ClosableMDIChildForm cmdif in this.MdiChildren)
{
    if (!cmdif.IsOkToClose())
    {
        e.Cancel = true;
        ((Form)mdifc).Focus();
        break;
    }
}

显然, this.MdiChildren 表单必须实现 ClosableMDIChildForm 接口。决定是否可以关闭窗口的逻辑在 IsOkToClose() 的实现中。

When closing the MDI main form, all child Close events are called first so, when calling frm.Close() on the foreach loop, Close events for the child are called again (I don't know why if they should be already closed).

ho1 suggestion worked pretty well for me. Here is my modified foreach loop (ClosableMDIChildForm is an interface which contains the IsOkToClose() method):

foreach (ClosableMDIChildForm cmdif in this.MdiChildren)
{
    if (!cmdif.IsOkToClose())
    {
        e.Cancel = true;
        ((Form)mdifc).Focus();
        break;
    }
}

Obviously, this.MdiChildren forms must implement ClosableMDIChildForm interface. The logic to decide if it's OK to close the window goes in the implementation of IsOkToClose().

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