Solved

Bug in Control.MouseMove ?

Posted on 2009-04-06
10
279 Views
Last Modified: 2013-11-26
Very easy to recreate.

Hover the mouse over the form, and without moving the mouse at all simply click the mouse button and you will notice the counter increase when you release the button.

Does anyone know why it fires the event? I also have noticed that some controls not only fire the event when the mouse button is released, but also when its pressed. So it fires twice for each mouse click.

Surely this is not by design. Microsoft describes this event as "Occurs when the mouse pointer is moved over the control."

There is no mention of it occuring when the user clicks a button.
Private cnt As Integer
 

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

        cnt = cnt + 1

        Me.Text = "MouseMove: " & cnt

End Sub

Open in new window

0
Comment
Question by:tfsln
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24083533
The granularity for MouseMove is pretty high, so it doesn't take much for the MouseMove event to fire.  It may not be incrementing because of the mouse click, but because the mouse moves...
0
 

Author Comment

by:tfsln
ID: 24083558
So your saying that i am actually moving my mouse, even though the pointer doesnt move a single pixel?
0
 
LVL 39

Expert Comment

by:appari
ID: 24083579
no sure i did this and Location is not changing, maybe it means click causes move event?

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        cnt = cnt + 1
        Me.Text = "MouseMove: " & cnt & " x:" & e.Location.X.ToString & " Y:" & e.Location.Y.ToString

    End Sub
0
 

Author Comment

by:tfsln
ID: 24083588
It doesnt state anywhere in the documentation (at least that i can see) that the mousemove event will be fired if the user clicks the mouse button.

Why would you want the mousemove event to fire when you release the mouse button? Isnt that what the MouseUp event is for?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24086436
As suggested, you could examine the process more closely by looking at the mouse coordinatesw to see that it doesn't take much for the MouseMove event to fire.  Events are asynchronous and you can have more than one event happen close together.  If you looked at the output from a "spy" application, you would see a serious amount of events, like MouseMove, MouseDown, MouseUp, ...
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 12

Expert Comment

by:omegaomega
ID: 24090026
I think that tfsln is correct about the MouseMove event firing without any actual move taking place.  I tried what TheLearnedOne suggests using the code in the snippet and (as shown in the output below) there is no change recorded in the mouse coordinates between the MouseUp event and the MouseMove event that immediately follows it.

11:40:14.125: {X=205,Y=238} in Form1_MouseMove.
11:40:14.140: {X=204,Y=238} in Form1_MouseMove.
11:40:14.156: {X=203,Y=238} in Form1_MouseMove.
11:40:14.187: {X=203,Y=237} in Form1_MouseMove.
11:40:20.359: {X=203,Y=237} in Form1_MouseDown.
11:40:20.453: {X=203,Y=237} in Form1_Click.
11:40:20.453: {X=203,Y=237} in Form1_MouseUp.
11:40:20.468: {X=203,Y=237} in Form1_MouseMove.
11:40:31.390: {X=203,Y=237} in Form1_MouseMove.
11:40:32.328: {X=203,Y=237} in Form1_MouseMove.

The final two MouseMove events are also interesting.  These occurred not as a result of moving the mouse, but as a result of switching context to another application.  Further testing shows another MouseMove event is generated if the mouse is over the test application when it is re-activated.  (I wonder if the MouseMove event would fire if the control moves under the mouse.  I might have to try that.)

I suppose that these extra events are probably for our convenience.  Although they have never caused me a problem, they apparently are a nuisance for tfsln.  

One inelegant possibility would be to simply record the mouse location in the event and then skip the processing that is causing a problem if the coordinates haven't changed.

Cheers,
Randy
0
 
LVL 12

Expert Comment

by:omegaomega
ID: 24090046
Oops -- I seem to have missed the code in the snippet - sorry, here it is now.
Public Class Form1
 

    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

        Debug.WriteLine(Now.ToString("HH:mm:ss.fff") & ": " & Control.MousePosition.ToString & " in Form1_Click.")

    End Sub
 

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown

        Debug.WriteLine(Now.ToString("HH:mm:ss.fff") & ": " & Control.MousePosition.ToString & " in Form1_MouseDown.")

    End Sub
 

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

        Debug.WriteLine(Now.ToString("HH:mm:ss.fff") & ": " & Control.MousePosition.ToString & " in Form1_MouseMove.")

    End Sub
 

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp

        Debug.WriteLine(Now.ToString("HH:mm:ss.fff") & ": " & Control.MousePosition.ToString & " in Form1_MouseUp.")

    End Sub
 

End Class

Open in new window

0
 
LVL 12

Expert Comment

by:omegaomega
ID: 24090135
Adding a timer to the code in the snippet and using it to move the form shows that the MouseMove event is also fired when the control moves underneath the mouse pointer.

12:01:54.984: {X=547,Y=233} in Timer1_Tick.
12:01:55.984: {X=547,Y=233} in Timer1_Tick.
12:01:56.984: {X=547,Y=233} in Timer1_Tick.
12:01:57.984: {X=547,Y=233} in Timer1_Tick.
12:01:58.984: {X=547,Y=233} in Timer1_Tick.
12:01:58.984: {X=547,Y=233} in Form1_MouseMove.
12:01:59.984: {X=547,Y=233} in Timer1_Tick.
12:01:59.984: {X=547,Y=233} in Form1_MouseMove.
12:02:00.984: {X=547,Y=233} in Timer1_Tick.
12:02:00.984: {X=547,Y=233} in Form1_MouseMove.
...
12:02:10.984: {X=547,Y=233} in Timer1_Tick.
12:02:10.984: {X=547,Y=233} in Form1_MouseMove.
12:02:11.984: {X=547,Y=233} in Timer1_Tick.
12:02:11.984: {X=547,Y=233} in Form1_MouseMove.
12:02:12.984: {X=547,Y=233} in Timer1_Tick.
12:02:13.984: {X=547,Y=233} in Timer1_Tick.
12:02:14.984: {X=547,Y=233} in Timer1_Tick.
12:02:15.984: {X=547,Y=233} in Timer1_Tick.

Cheers,
Randy

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Debug.WriteLine(Now.ToString("HH:mm:ss.fff") & ": " & Control.MousePosition.ToString & " in Timer1_Tick.")

        Me.Left = (20 + Me.Left) Mod 1000

    End Sub

Open in new window

0
 

Author Comment

by:tfsln
ID: 24092101
> One inelegant possibility would be to simply record the mouse location in the event and then skip the processing that is causing a problem if the coordinates haven't changed.

That is actually a good idea. Still, it would be better if the event worked the way it was supposed to! Maybe i should ask microsoft... but it can be extremely difficult getting answers
0
 

Accepted Solution

by:
tfsln earned 0 total points
ID: 24153986
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now