6

Why does MouseMove event of a PictureBox seem to be fired continuously even if the mouse is not moved? I have tried the following codes to prove it (by simply creating a new form with a PictureBox and a Label on it).

private void pictureBox1_MouseMove ( object sender, MouseEventArgs e )
{
  label1.Text = DateTime.Now.ToLongTimeString ( ) + ": " + e.X + "," + e.Y;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Setyo N
  • 1,953
  • 2
  • 26
  • 28
  • May be your form mousemove event register this method?! – Reza ArabQaeni Dec 12 '11 at 23:24
  • "Continuously" as in "all the time that it's running" or just during a certain period of time? Are the events erratic? (If so, suspect vibrations and an over-sensitive mouse.) – Ry- Dec 12 '11 at 23:26
  • 2
    You can try running Spy++ and monitoring the Windows messages to try to see what else is happening. If you have Visual Studio, it should have spyxx.exe in one of the Tools folders. Additionally, try physically unplugging your mouse while debugging and see if you're still getting MouseMove messages. – Trevor Elliott Dec 12 '11 at 23:46
  • I've got a chance to run the same codes in a different PC and it cannot reproduce. Something must be wrong with the first PC. I will try to use Spy++ as Moohze suggested. Thanks! – Setyo N Dec 13 '11 at 02:27
  • slanted table? remind me never to play poker against you:) – Jeremy Thompson Dec 13 '11 at 04:02

4 Answers4

5

Certain methods involving windows forms cause some level of internal refresh of the form, which (by my estimation) cause the form to 'pick up' the mouse position and thus fire the MouseMove event. One such method is associating a toolTip with the form. As a result,

int moveCount = 0;
ToolTip toolTip = new ToolTip();
private void form1_MouseMove(object sender, MouseEventArgs e)
{
    Trace.WriteLine(moveCount);
    moveCount++;
    toolTip.SetToolTip(this, "Hello world");
}

will fire continuously even if the mouse is not moved, while

int moveCount = 0;
private void form1_MouseMove(object sender, MouseEventArgs e)
{
    Trace.WriteLine(moveCount);
    moveCount++;
}

will fire only when the mouse is actually moved.

Having a look inside your MouseMove event for something that touches the form in a 'deep' way might help to reveal the source of the looping behavior.

NiloCK
  • 571
  • 1
  • 11
  • 33
4

"pictureBox1_MouseMove" is just a delegate function. So with your code we can only assume that it was attached to MouseMove, and only MouseMove, in the designer.

Double check all references for "pictureBox1_MouseMove", and also keep in mind that Windows fires MouseMove messages on mouse click even if you don't move the mouse.

Worst case scenario you could store the Point e.Location in a local member variable ("oldLocation") and verify that the mouse actually moved before processing your command:

private Point oldLocation = Point.Empty;

private void pictureBox1_MouseMove ( object sender, MouseEventArgs e )
{
    if (e.Location != oldLocation)
    {
        oldLocation = e.Location;

        label1.Text = DateTime.Now.ToLongTimeString ( ) + ": " + e.X + "," + e.Y;
    }
}
Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
1

First guess... either seismic activity or nargles - I suspect nargles :).

Using the code sample you provided I do not get the same behavior. My label only updates if the mouse is actually moving. (and the pointer has to be over the picturebox to boot).

But seriously... are there any vibrations on your desk? I might also try another mouse - as the circuitry inside might be sending bad data.

RThomas
  • 10,702
  • 2
  • 48
  • 61
  • 1
    No, I make sure the mouse position does not change by showing e.X and e.Y in the label, but strangely the time information in the label changes in a rate like once per second. Additional info: using VS 2008 SP1 on Vista SP2. Could it be Windows/VS/.NET bug in my PC? – Setyo N Dec 12 '11 at 23:33
0

An observation: If the MouseMove event handler code modifies the size of a control (either explicitly or implicitly per the AutoSize property) in either direction, the MouseMove event handler will be re-invoked despite no actual mouse movement. This could result in a vicious cycle. If the width of the auto-sized control is stable despite its contents being changed, the MouseEvent handler will not be re-invoked.