Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在关闭MDI窗体和子窗体时正确提示用户?_C#_.net_Winforms_Mdi - Fatal编程技术网

C# 如何在关闭MDI窗体和子窗体时正确提示用户?

C# 如何在关闭MDI窗体和子窗体时正确提示用户?,c#,.net,winforms,mdi,C#,.net,Winforms,Mdi,我正在编写一个IRC客户端,其中有一个带有服务器和通道窗口的MDI父级。当您关闭服务器窗口时,它会提示用户,如果用户想要关闭它,则会关闭与服务器的连接,等等 我希望MDI父级关闭时只有一个提示,而不是每个服务器都有一个提示问题是,当用户尝试关闭父窗体时,子窗体的OnFormClosing在父窗体的OnFormClosing之前被调用。将MDI子窗体的FormClosing事件修改为以下内容: private void MyChildForm_FormClosing(object sender,

我正在编写一个IRC客户端,其中有一个带有服务器和通道窗口的MDI父级。当您关闭服务器窗口时,它会提示用户,如果用户想要关闭它,则会关闭与服务器的连接,等等


我希望MDI父级关闭时只有一个提示,而不是每个服务器都有一个提示问题是,当用户尝试关闭父窗体时,子窗体的OnFormClosing在父窗体的OnFormClosing之前被调用。

将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
}

然后将全局关闭提示/逻辑放入MDI父窗体的
FormClosing
事件中。提示:将
this.MdiChildren
与窗口类型测试结合使用,即
childForm是IServorm

另一个选项是使用MDI的DefWndProc捕捉子窗口之前的MDI“close”,并“kill”那里的子窗口

''' <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
''在关闭开始前拦截用户单击关闭按钮(或ALT+F4'ing)。
受保护的覆盖子DefWndProc(ByRef m As System.Windows.Forms.Message)
尝试
Const SC_CLOSE=&HF060'http://msdn.microsoft.com/en-us/library/ms646360%28v=vs.85%29.aspx
If(m.Msg=WndMsg.WM_SYSCOMMAND)_
并且(m.WParam.ToInt32=SC_CLOSE)然后
如果(不是Me.ExitApplicationPrompt()),则在此处执行“关闭子窗体”
m、 Msg=0'取消关闭命令
如果结束
如果结束
特例
My.ExceptionHandler.HandleClientError(ex)
结束尝试
MyBase.DefWndProc(m)
端接头