Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

Bug in Control.MouseMove ?

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
tfsln
Asked:
tfsln
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Bob LearnedCommented:
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
 
tfslnAuthor Commented:
So your saying that i am actually moving my mouse, even though the pointer doesnt move a single pixel?
0
 
appariCommented:
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
Independent Software Vendors: 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!

 
tfslnAuthor Commented:
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
 
Bob LearnedCommented:
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
 
omegaomegaDeveloperCommented:
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
 
omegaomegaDeveloperCommented:
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
 
omegaomegaDeveloperCommented:
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
 
tfslnAuthor Commented:
> 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
 
tfslnAuthor Commented:
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now