Form1: OnMouseLeave

C

Colin McGuire

Hi again NG, this question follows from a similar question that I have
thought through a bit more, and the next problem I encountered :(

Here is the issue clearly: if I have form1 with a button on it, and in my
form1 source-code I put the following code


Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
Debug.WriteLine("OnMouseLeave")
MyBase.OnMouseLeave(e)
End Sub


then as I move my mouse cursor over the button, the VS IDE output/debug
window displays "OnMouseLeave". Also as I move the mouse cursor off the
form (over something else, possibly not even one of my applications), the
output/debug window also displays "OnMouseLeave".

What I am wanting to do is hide the form if the mouse cursor moves off it
(but not to one of the controls on the form).

My question is how on earth can this be achieved given that the MouseLeave
event is fired for both situations (I want to execute Me.Hide() only when
the mouse really moves off the form, not when it moves onto some of the
controls on the form). There is another event I should be overriding rather
than OnMouseLeave?

Colin
 
J

Jay B. Harlow [MVP - Outlook]

Colin,
First I would like to ask why, as most of the time if a form disappeared
when I accidentally moved the mouse off of it, I would probably uninstall
the software... ;-)

As you found if you move the mouse over a child control, the mouse is now
over the child control. What I would suggest is you handle the MouseEnter &
MouseLeave event for each of your child controls also. You can use
AddHandler & a loop over the Controls collection to attach all your
children's mouse events to a single handler. Remember that child controls
can have children, so you will need to make this recursive.

I would consider incrementing a counter on MouseEnter, decrementing the
counter on MouseLeave, when the count is zero I am off the form & children.

Normally derived controls/forms should override OnMouseLeave, however
because you want to track the children also, I would make the form use the
MouseLeave event instead of overriding OnMouseLeave.

I suspect there may be a windows hook you can use, but I have not used any
windows hooks.

You may also want to ask this "down the hall" in the
microsoft.public.dotnet.framework.windowsforms or
microsoft.public.dotnet.framework.windowsforms.controls newsgroups.

Hope this helps
Jay
 
F

Fergus Cooney

Hi Colin,

I very rarely counter Jay's advice, but Enter/Leave handlers for every
control shouldn't be necessary.

I would consider obtaining the mouse co-ordinates and determining whether
the mouse is still above your Form

In MouseLeave you could get the mouse co-ordinates and convert them to
screen co-ordinates. These could then be tested against the Form's Location
and Size. But!

I wouldn't do it this myself. As Jay suggests, a disappearing Form can be
disconcerting. I accept, however, that there are legitimate reasons and my
concern would be the User-friendliness within this scenario.

If I were to <accidently> move off a Form that I know is going to
disappear, I would be cursing the effort required to get it back, and be
thinking 'why couldn't it have just waited a liitle while'.

So that's what I would implement. Setting a Timer in MouseLeave (maybe
half a second) and getting the mouse co-ordinates in the Tick handler would do
the job. If the mouse is out, so goes the Form.

Your next question will be how do I get the mouse position such that I can
test it against the Form. I don't have the answer to that to hand so I'm just
posting the method for now. You may want to repost that as a separate question
(here and/or in the windowsforms groups that Jay gave you).

Regards,
Fergus
 
J

Jay B. Harlow [MVP - Outlook]

Fergus,
Ha! take the easy road!

I figured there was an easier method, all I was thinking was windows hook...

Thanks
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top