我试图让用户有机会通过抛出“你确定吗?”来取消MDI Parent的FormClosing。带有e.cancel的msgbox但是当MDI Parent调用FormClosing时,所有MDI子项在msgbox出现之前先关闭。
我想知道是否有一种更简单的方法来防止MDI儿童关闭而不是必须对每个儿童表格的FormClosing进行e.cancel,直到我得到一个积极的回应来关闭,然后推动孩子们的所有近距离事件,因为这似乎是如果你有很多MDI儿童,那就太麻烦了。
编辑:我想我能找到的唯一解决方案是添加 如果e.CloseReason = CloseReason.MdiFormClosing那么e.Cancel = True 到FormClosing事件并改为使用ApplicationExit。
答案 0 :(得分:0)
If e.CloseReason = CloseReason.MdiFormClosing Then
e.Cancel = True
End If
在您关闭应用程序时,您应该可以使用Application.Exit
答案 1 :(得分:0)
从父母
开始表单 frmMdiChild1.MdiParent = Me
frmMdiChild1.Show()
添加Formclosing Sub
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也会阻止父母关闭(lol)。
(vb.net 2010)
答案 2 :(得分:0)
Public Class clsGlobalVariables
Public Shared mdi_main As mdiMain
结束班
导入System.Windows.Forms
Public Class 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
结束班
Public Class 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
结束班