Solved

MouseMove event firing even when mouse not moving

Posted on 2009-04-05
12
844 Views
Last Modified: 2013-11-26
If i hover my mouse over the DV object, and click the mouse button - this event fires.

Why? It should only fire when the mouse moves right? Why is it firing when i hover the mouse over the control, and then click the button without moving the mouse?

Im trying to get the drag and drop and double click functionality to co-exist but this is causing an issue with the double click event not firing properly with this code not working as expected.
Private Sub DV_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DV.MouseMove

        If e.Button = Windows.Forms.MouseButtons.Left Then

            'Once the drag operation starts, the mousemove no longer fires and is replaced by the DragOver event. This means we dont have to check whether there is a drag operation in progress first

            Debug.WriteLine("DragDrop")

            StartDragDrop()

        End If

    End Sub

Open in new window

0
Comment
Question by:tfsln
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 7

Expert Comment

by:HalfAsleep
Comment Utility
And you are sure you are not moving the mouse?  Do you get the click event also, or just the move event? Sometimes, it is easy to move the mouse as you are clicking it, but you should still get the click event, are you getting the click event?
Also, there are about 4 or 5 events custom made to support dragging operations. If you are trying to support drag and drop, you should have a look at them.
0
 

Author Comment

by:tfsln
Comment Utility
I am not moving the mouse, that is a guarentee. The event is firing when you hover the mouse over, then click the button - the event seems to be triggered by the mouse click.

I am familiar with drag operations. You need an event to start the drag operation which is typically the mousedown event (but that wont work because it interferes with the double click event - and so this mousemove event is my backup plan, but that seems to have the same behaviour as the mousedown event which appears to me to be incorrect)
0
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
If you want to implement a drag-drop you don't need that event. You have some specific for that!
Check this simple example
http://www.vb-helper.com/howto_net_drag_drop.html
0
 

Author Comment

by:tfsln
Comment Utility
What example uses the mousedown event. If you start a drag operation in the mousedown event, then the doubleclick event for that control stops working correctly.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
That's a good question...and I'm not sure why.  ...but I am able to get both DoubleClick and a "drag" to work at the same time though with the code below.  =\

I did notice that the even when the mouse is just sitting there I get "random" MouseMove events every 10 - 20 seconds.  I LITERALLY let go of the mouse and the number on the form increments by 2 every so often.  And no, the desk isn't vibrating or being bumped...no earthquakes or anything...  =)
Public Class Form1
 

    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

        If e.Button = Windows.Forms.MouseButtons.Left Then

            Me.DoDragDrop(Me, DragDropEffects.All)

        End If

    End Sub
 

    Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick

        MessageBox.Show("DoubleClick")

    End Sub
 

End Class

Open in new window

0
 
LVL 12

Expert Comment

by:omegaomega
Comment Utility
Hello, tfsln,

You might also find the SystemInformation.DragSize Property to be helpful.  It won't prevent the spurious move events, but it may help in preventing the initiation of unwanted drag operations resulting from small mouse movements.

Cheers,
Randy
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:tfsln
Comment Utility
So i would need to do something like record when the mouse was pressed down, and use the mousemove event to establish whether they have dragged the cursor outside the bounds of SystemInformation.DragSize?

That seems like alot of work. This would not be a problem if the MouseMove event was not raised when the mouse button is clicked. Isnt it frustrating when you hit a road block because one of the most fundamental basics in your coding environment has a bug in it? This is the kind of stuff i get from infragistics. Microsoft are usually pretty good.

SystemInformation.DragSize doesnt really help me, i mean, it just returns a Size object. It doesnt really make the solution any easier unless im missing something?
0
 
LVL 12

Accepted Solution

by:
omegaomega earned 500 total points
Comment Utility
Hi, tfsln,

Re: "That seems like alot of work."

I suppose any work that "shouldn't" be required is always too much.  But really, I don't find this so hard.  I create a (module scope) "tolerance" rectangle in the MouseDown event (one instruction):

     mDragTolerance = New Rectangle(e.X - CInt(SystemInformation.DragSize.Width / 2), _
                                                       e.Y - CInt(SystemInformation.DragSize.Height / 2), _
                                                       SystemInformation.DragSize.Width, _
                                                       SystemInformation.DragSize.Height)

and then test the mouse location against it in the MouseMove event:

     If (Rectangle.op_Inequality(mdreDragTolerance, Rectangle.Empty) AndAlso _
                            Not mdreDragTolerance.Contains(e.X, e.Y)) Then
          ...

(two more instructions, if you include the terminating End If).   I do find it strange (and inconvenient) that it is only DragSize that is available and not a rectangle of that size.  (The documentation for DragSize even refers to a rectangle centred on the mouse position.)

Re: "Isnt it frustrating..."

Yep.  But I suppose that's why they pay us the big bucks.  ;-)

Re: "SystemInformation.DragSize doesnt really help me..."

You're right.  IdleMind's solution is (as usual) perfectly fine without it (and deserves full credit).  But using DragSize might make the drags that your application does a tiny bit more consistent with those performed by Windows itself.

Cheers,
Randy

0
 

Author Comment

by:tfsln
Comment Utility
Ok thanks for that. I've never really needed to go into rectangles and drawing with the applications ive developed thus far. But using your example i think i know what to do now.

Idle_Mind's solution does not work for me because with the control i am using (an infragistics UltraDayView control) behaves even weirder.

Notice in Idle_Mind's example, the counter increases only when the mouse button is released. With the UltraDayView control, the mousemove is fired when the mouse button is pressed AND when its released.

Extremely annoying. Seriously i dont know about anyone else, but infragistics are the worst company ive ever dealt with in terms of bugs.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Never used any third party controls...  =\
0
 
LVL 12

Expert Comment

by:omegaomega
Comment Utility
Hello, tfsln,

I too avoid third party controls.  (I seem to recall getting into difficulties with them in some previous life.)  And I have no experience at all with infragistics controls.

This is just wild speculation, but I wonder if you could make any gains by creating your own control (maybe a UserControl) and then adding the infragistics control to it.  Then perhaps you could control whether or not MouseMove events were handled by your control, or passed on to the infragistics control for processing.  This way you might be able to get more normal MouseMove behaviour.

(And you were worried that DragSize was a lot of work... :-(  )

Cheers,
Randy
0
 

Author Comment

by:tfsln
Comment Utility
Nah i think i have all the information i need from this question. I've actually posted another question about the MouseMove problem. I think ill try your suggestion though.

Thanks for your time
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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

15 Experts available now in Live!

Get 1:1 Help Now