1

我试图让用户有机会通过抛出“你确定吗?”来取消 MDI Parent 的 FormClosing。带有 e.cancel 的 msgbox 但是,当 MDI Parent 调用 FormClosing 时,所有 MDI Children 首先关闭,然后才出现 msgbox。

我想知道是否有一种更简单的方法可以防止 MDI 儿童关闭,而不是必须取消每个子窗体的 FormClosing 直到我得到对关闭的积极响应,然后将所有关闭事件推送给孩子,因为这似乎太多了如果你有很多 MDI 孩子,那就麻烦了。

编辑:我想我能找到的唯一解决方案是将 If e.CloseReason = CloseReason.MdiFormClosing Then e.Cancel = True 添加到 FormClosing 事件并改用 ApplicationExit 。

4

3 回答 3

0

从父表单开始

    frmMdiChild1.MdiParent = Me
    frmMdiChild1.Show()

添加表单关闭子

Private Sub frmMdiChild1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If e.CloseReason = CloseReason.UserClosing Then
        e.Cancel = True
    End If
End Sub

@Theveloper:我试过 .MdiFormClosing 但它没有用。为了找出使用什么,我做了一个 MsgBox(e.CloseReason)。此外,只有 e.Cancel = True 也会阻止父级关闭(大声笑)。

(vb.net 2010)

于 2012-10-26T14:38:31.767 回答
0
If e.CloseReason = CloseReason.MdiFormClosing Then
   e.Cancel = True
End If

应该完成这项工作,之后您可以在想要关闭应用程序时使用 Application.Exit

于 2011-10-19T07:39:31.770 回答
0

公共类 clsGlobalVariables

Public Shared mdi_main As mdiMain

结束类

导入 System.Windows.Forms

公共类 mdiMain

Public forced_close As Boolean = False

Private Sub mdiMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    e.Cancel = True
    If MsgBox("Are you sure you want to exit?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
        ' Close all child forms of the parent.
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
        e.Cancel = False
    End If
End Sub

结束类

公开课 frmMember

Private Sub frmMember_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    Select Case e.CloseReason
        Case CloseReason.UserClosing
            e.Cancel = True
            If Not clsGlobalVariables.mdi_main.forced_close Then
                If MsgBox("Are you sure you want to close?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    e.Cancel = False
                End If
            End If
        Case Else
            clsGlobalVariables.mdi_main.forced_close = True
            e.Cancel = True
    End Select
End Sub

结束类

于 2017-03-15T08:51:18.990 回答