Solved

Bug in Control.MouseMove ?

Posted on 2009-04-06
10
285 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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