Solved

Bug in Control.MouseMove ?

Posted on 2009-04-06
10
288 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…

734 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