代码之家  ›  专栏  ›  技术社区  ›  Brian Ortiz

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

  •  1
  • Brian Ortiz  · 技术社区  · 16 年前

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

    问题是,当用户试图关闭父窗体时,子窗体的OnFormClosing将在父窗体之前调用。

    2 回复  |  直到 16 年前
        1
  •  0
  •   Jon Seigel    15 年前

    修改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父窗体的 合模 事件。提示:使用 this.MdiChildren 结合窗型试验,即。, childForm is IServerForm .

        2
  •  0
  •   AndrewD    13 年前

    ''' <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
    
    推荐文章