Setting Form to Hide using Deactivate for VB.Net app

I am developing a form with a tray icon.

I want it so you can click on the tray icon to show the form or hide the form.
I also want it so if you click away from the form (eg: the desktop) it also hides the form.

Now, using the form event "deactivate" I can put a Me.Hide() to hide the form.

The tray icon click event has code like the following:
if Me.Visible Then
End If

Open in new window

The problem I have is, when the form is visible, and I click the tray icon, the deactivate event fires, which hides the form, THEN the form click event fires which reactivates it.

How to I tell the deactivate event not to run me.Hide() if the tray icon is clicked. (preferrable)
Alternatively, how do I tell the tray icon click event not to run the code if deactivate just hid the form
(i'd rather not use timers)

When the form is active, looking at the event order (with a debug window) the event chain is:
tray mousedown
tray mouseup
tray mouseclick

Open in new window

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.

DFPITCAuthor Commented:
I've ended up setting a global var called etime (event time)
When the deactivate event fires, or the tray icon click event fires it sets the etime to the current time.
On the deactivate event and the tray icon click event, I compare etime to now to make sure at least 100ms has passed.
This ensures that the form doesn't hide and show itself too close together (eg: when both events fired)

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
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.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.