VB6中有个Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)的方法可以捕获窗体关闭时间,查了一下MSDN,UnloadMode有几个枚举值:
使用如下简单代码:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Select Case UnloadMode
Case vbFormControlMenu
MsgBox "vbFormControlMenu"
Case vbFormCode
MsgBox "vbFormCode"
Case vbAppWindows
MsgBox "vbAppWindows"
End Select
End Sub
Canel的值要是0就可以关闭,要是其他值,关闭无效,不明白干嘛不直接设Cancel为布尔变量。
但是也就能试试几个原因
在.NET下,这种捕获升级到CloseReason这个变量,如下
Private Sub Form_closing(ByVal sender As Object, ByVal e As Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case e.CloseReason
Case CloseReason.UserClosing
MsgBox("Close form use X")
Case CloseReason.FormOwnerClosing
MsgBox("Close form inner")
Case CloseReason.None
MsgBox("unkown reason")
Case CloseReason.TaskManagerClosing
If MsgBox("The task manager do", MsgBoxStyle.YesNo, Me.Text) = MsgBoxResult.No Then
e.Cancel = 1
End If
Case CloseReason.ApplicationExitCall
MsgBox("Out")
End Select
End Sub
比较简单,以前不知道,记录下来...
Constant | Value | Description |
vbFormControlMenu | 0 | The user chose the Close command from the Control menu on the form. |
vbFormCode | 1 | The Unload statement is invoked from code. |
vbAppWindows | 2 | The current Microsoft Windows operating environment session is ending. |
vbAppTaskManager | 3 | The Microsoft Windows Task Manager is closing the application. |
vbFormMDIForm | 4 | An MDI child form is closing because the MDI form is closing. |
vbFormOwner | 5 | A form is closing because its owner is closing. |
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Select Case UnloadMode
Case vbFormControlMenu
MsgBox "vbFormControlMenu"
Case vbFormCode
MsgBox "vbFormCode"
Case vbAppWindows
MsgBox "vbAppWindows"
End Select
End Sub
Canel的值要是0就可以关闭,要是其他值,关闭无效,不明白干嘛不直接设Cancel为布尔变量。
但是也就能试试几个原因
在.NET下,这种捕获升级到CloseReason这个变量,如下
Private Sub Form_closing(ByVal sender As Object, ByVal e As Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case e.CloseReason
Case CloseReason.UserClosing
MsgBox("Close form use X")
Case CloseReason.FormOwnerClosing
MsgBox("Close form inner")
Case CloseReason.None
MsgBox("unkown reason")
Case CloseReason.TaskManagerClosing
If MsgBox("The task manager do", MsgBoxStyle.YesNo, Me.Text) = MsgBoxResult.No Then
e.Cancel = 1
End If
Case CloseReason.ApplicationExitCall
MsgBox("Out")
End Select
End Sub
比较简单,以前不知道,记录下来...