over load close function button?

Is there a way to overload the close button that is on the top right corner or most windows programs.  I have an application that uses the system tray.  I want to make it so that when the user clicks on the close button it doesn't really close the program.  I want it to just minimize or hide it.  but i still want it running until the user right clicks on the system tray item and selects exit.
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.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

If (UnloadMode = 0) Then Cancel = 1
    Visible = False
End Sub
You need to put code in the QueryUnload Event of your form.  There is a variable that gets passed into this routine when a user tries to close the form.. UNLOADMODE...

You can look a UNLOADMODE and do whatever you want...  You can also set the CANCEL to TRUE and the program will not terminate.

UNLOADMODE can be one of the following and you can test for it:

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.


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
ueihpAuthor Commented:
Thanks!  =)
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.