Solved

Bug in Control.MouseMove ?

Posted on 2009-04-06
10
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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