Determine s Shutdown|Close All Programs

I have a little program that runs in the system tray.  In the Form_Unload event I have a confirmation message box, which asks the user if they really want to exit.  However, if the User does a shutdown, restart, or "Close All Programs and Login in as another user" it still gives the confirmation msgbox.  I don't want this to happen.  So how do I tell if the user or the system is trying to shut the programs down?

:) dapperry
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use the QueryUnload event instead of the Unload event. In there you can change the value of the variable Cancel to stop the program from quitting. The helpfile will tell you how to change it, it's all there. ;)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You can tell how the unload was invoked by looking at the UnloadMode parameter in QueryUnload.  In your case, you want to confirm exit in vbFormControlMenu and vbFormCode, but not in vbAppWindows.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Select Case UnloadMode
    Case vbFormControlMenu
        'The user chose the Close command from the Control menu on the form.
    Case vbFormCode
        'The Unload statement is invoked from code.
    Case vbAppWindows
        'The current Microsoft Windows operating environment session is ending.
    Case vbAppTaskManager
        'The Microsoft Windows Task Manager is closing the application.
    Case vbFormMDIForm
        'An MDI child form is closing
    End Select
End Sub
dapperryAuthor Commented:
Thanks,  I guess thats one form event I just never got around to looking at!  

:) dapperry
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.