Home » Forms » How to find the user is trying to close the application using task manager or by pressing the X in Title bar, etc. How to restrict/refuse that
How to find the user is trying to close the application using task manager or by pressing the X in Title bar, etc. How to restrict/refuse that
Posted on July 18, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 'Forms - How to find the user is trying to close the application using task manager or by pressing the X in Title bar, etc. How to restrict/refuse that 'When the form is about to be unloaded, the form object receives a QueryUnload 'event. You can learn why a form is unloading by examining the UnloadMode 'parameter. I have created this code skeleton, which I reuse in my 'applications as necessary: Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Select Case UnloadMode Case vbFormControlMenu ' = 0 ' Form is being closed by user. Case vbFormCode ' = 1 ' Form is being closed by code. Case vbAppWindows ' = 2 ' The current Windows session is ending. Case vbAppTaskManager ' = 3 ' Task Manager is closing this application. Case vbFormMDIForm ' = 4 ' MDI parent is closing this form. Case vbFormOwner ' = 5 ' The owner form is closing. End Select End Sub 'You can refuse to unload by setting the Cancel parameter to True, as in the following code: Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) ' Don't let the user close this form. Select Case UnloadMode Case vbFormControlMenu, vbAppTaskManager Cancel = True End Select End Sub |
Enjoy this article?
Filed under: Forms
Leave a comment