简体   繁体   English

关闭MDI表单和子级时如何正确提示用户?

[英]How to properly prompt the user when closing an MDI form and children?

I'm writing an IRC client, where there is an MDI parent with Server and Channel windows. 我正在编写一个IRC客户端,其中有一个带有服务器和通道窗口的MDI父级。 When you close a Server window, it prompts the user and if they want to close it, the connection to the server is closed etc. 当您关闭“服务器”窗口时,它会提示用户,如果他们要关闭该窗口,则会关闭与服务器的连接等。

I would like there to be only one prompt when the MDI parent is closed rather than a prompt for each server. 我希望关闭MDI父级时只有一个提示,而不是每个服务器都有一个提示。 The problem is when the user tries to close the parent, the child Forms' OnFormClosing is called before the parent's. 问题是,当用户尝试关闭父窗体时,子窗体的OnFormClosing在父窗体之前被调用。

Another option is to catch the MDI "close" BEFORE the child windows using the MDI's DefWndProc and "kill" the child windows there. 另一种选择是使用MDI的DefWndProc在子窗口之前捕获MDI,然后在子窗口中“杀死”子窗口。

''' <remarks>Intercept the user clicking on the CLOSE button (or ALT+F4'ing) before the closing starts.</remarks>
Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)

    Try
        Const SC_CLOSE = &HF060 'http://msdn.microsoft.com/en-us/library/ms646360%28v=vs.85%29.aspx

        If (m.Msg = WndMsg.WM_SYSCOMMAND) _
        AndAlso (m.WParam.ToInt32 = SC_CLOSE) Then

            If (Not Me.ExitApplicationPrompt()) Then ' Do your "close child forms" here
                m.Msg = 0 'Cancel the CLOSE command
            End If

        End If

    Catch ex As Exception
        My.ExceptionHandler.HandleClientError(ex)
    End Try

    MyBase.DefWndProc(m)

End Sub

Modify the MDI child form's FormClosing event to the following: 将MDI子窗体的FormClosing事件修改为以下内容:

private void MyChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.MdiFormClosing)
    {
        e.Cancel = true;
        return;
    }

    // Child window closing code goes here
}

And then put your global closing prompt/logic in the MDI parent form's FormClosing event. 然后将全局关闭提示/逻辑放在MDI父窗体的FormClosing事件中。 Hint: use this.MdiChildren in combination with window type tests, ie, childForm is IServerForm . 提示:与窗口类型测试结合使用this.MdiChildren ,即childForm is IServerForm

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

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