Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If MsgBox("未保存,确定退出?", vbOKCancel, "提示") = vbCancel Then
Cancel = 1
End If
End Sub
效果如图:
把一楼的改一下,就变成了关不了的窗口😄
Private Sub Form_Unload(Cancel As Integer)
myexit = MsgBox("看完才能退出哦", vbExclamation + vbYesNo + vbDefaultButton, "哼...")
Cancel = True
End Sub
在关闭窗体的时候触发几个事件,可以在两个事件过程(sub)中来处理
首先是 Form_QueryUnload,其次是 Form_Unload,取其一即可
可以自己复制代码测试以下
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Cancel = MsgBox("您果真要退出?", vbYesNo, "来自:Form_QueryUnload") = vbNo
End Sub
Private Sub Form_Unload(Cancel As Integer)
Cancel = MsgBox("您果真要退出?", vbYesNo, "来自:Form_Unload") = vbNo
End Sub
看我的,给你优化一下:
Private Sub Form_Unload(Cancel As Integer)
myexit = MsgBox("您果真要退出?", vbExclamation + vbYesNo + vbDefaultButton2, "退出确认...")
If myexit = vbNo Then
Cancel = True
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim r As VbMsgBoxResult
r = MsgBox("是否真的关闭窗口?", vbOKCancel)
If r = vbOK Then
Cancel = -1
Else
Cancel = 0
End If
End Sub