MouseMove event in PictureBox is fired continuously even if the mouse is not moved

Setyo N picture Setyo N · Dec 13, 2011 · Viewed 7.1k times · Source

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;
}

Answer

NiloCK picture NiloCK · Dec 20, 2013

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.