【问题标题】:How to properly prompt the user when closing an MDI form and children?关闭 MDI 窗体和子窗体时如何正确提示用户?
【发布时间】:2009-12-12 23:06:02
【问题描述】:

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

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

【问题讨论】:

    标签: c# .net winforms mdi


    【解决方案1】:

    将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 is IServerForm

    【讨论】:

      【解决方案2】:

      另一种选择是在子窗口之前使用 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 2019-06-15
        • 2010-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多