Mouse Movement & Control Bounds

T

Tom

Hi

I am having problems working out if the mouse pointer is within the
control bounds within the OnMouseMove method:

Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)

Debug.WriteLine(Me.Bounds.Contains(e.X, e.Y))

End Sub

This always returns false even when the mouse is being moved over the
control. I would expect it only to return false if the mouse was being
moved outside the control when the mouse button was originally pressed
over the control.

MSDN suggests that i am using it correctly:

http://msdn.microsoft.com/library/d...systemdrawingrectangleclasscontainstopic1.asp

Anyone know where i am going wrong?

Thanks

Tom
 
A

Armin Zingler

Tom said:
Hi

I am having problems working out if the mouse pointer is within
the control bounds within the OnMouseMove method:

Protected Overrides Sub OnMouseMove(ByVal e As
MouseEventArgs)

Debug.WriteLine(Me.Bounds.Contains(e.X, e.Y))

End Sub

This always returns false even when the mouse is being moved over
the control. I would expect it only to return false if the mouse was
being moved outside the control when the mouse button was originally
pressed over the control.

MSDN suggests that i am using it correctly:

http://msdn.microsoft.com/
library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdr
awingrectangleclasscontai nstopic1.asp

Anyone know where i am going wrong?

Bounds returns coordinates relative to the parent of the control, whereas
e.x and e.y are relative to the top left corner of the control.

Use
Debug.WriteLine(Me.ClientRectangle.Contains(e.X, e.Y))

BTW, don't forget to call
MyBase.OnMouseMove(e)


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
T

Tom

Bounds returns coordinates relative to the parent of the control, whereas
e.x and e.y are relative to the top left corner of the control.

Use
Debug.WriteLine(Me.ClientRectangle.Contains(e.X, e.Y))

BTW, don't forget to call
MyBase.OnMouseMove(e)

Armin, that's fantastic, thanks mate.
 
H

Herfried K. Wagner [MVP]

* Tom said:
I am having problems working out if the mouse pointer is within the
control bounds within the OnMouseMove method:

Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)

Debug.WriteLine(Me.Bounds.Contains(e.X, e.Y))

End Sub

This always returns false even when the mouse is being moved over the
control. I would expect it only to return false if the mouse was being

Mouse events will only be generated for the control under the mouse, so
you won't receive an event when the mouse is moved outside the control.
 
T

Tom

Mouse events will only be generated for the control under the mouse, so
you won't receive an event when the mouse is moved outside the control.

If the mouse button is pressed over the control and remains pressed as
you move off the control, the event will still fire as the mouse
moves, likewise the OnMouseUp event.
 
A

Armin Zingler

Herfried K. Wagner said:
Mouse events will only be generated for the control under the mouse,
so you won't receive an event when the mouse is moved outside the
control.


No, it is also received outside as stated in the sentence you quoted only
the first half. ;-)

"I would expect it only to return false if the mouse was being
moved outside the control when the mouse button was originally pressed
over the control."
 
H

Herfried K. Wagner [MVP]

* Tom said:
If the mouse button is pressed over the control and remains pressed as
you move off the control, the event will still fire as the mouse
moves, likewise the OnMouseUp event.

OK, I didn't think of that. Thank you for making me aware that I missed
this case.
 
H

Herfried K. Wagner [MVP]

* "Armin Zingler said:
No, it is also received outside as stated in the sentence you quoted only
the first half. ;-)

"I would expect it only to return false if the mouse was being
moved outside the control when the mouse button was originally pressed
over the control."

Sorry :-(. I took 13 hours of sleep but I am still tired...
 

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