Mouse Movement & Control Bounds

  • Thread starter Thread starter Tom
  • Start date Start date
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
 
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
 
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.
 
* 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.
 
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.
 
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."
 
* 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.
 
* "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...
 
Back
Top