I have this problem that event called "MouseEnter" does not fire when mouse button is held down. How could i fix it?
+2
A:
That's by design. You can work around it by using, say, MouseMove:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point pt = TargetControl.PointToClient(Cursor.Position);
Rectangle rc = TargetControl.ClientRectangle;
if (rc.Contains(pt))
{
// do what would be done on MouseEnter
}
}
}
This is not ideal, though - if the mouse button is pressed when the mouse is hovering over another control on the form, then it doesn't appear in the MouseMove event that the button is pressed (as @Hans pointed out, the other control 'Captures' the MouseDown). If that's a problem, then combining the hit test in MouseMove while separately tracking MouseDown and MouseUp on the form should work.
Stuart Dunkeld
2010-08-11 11:48:56
There won't be any MouseMove messages either.
Hans Passant
2010-08-11 12:58:58
@Hans - yes there are, I tested it worked..
Stuart Dunkeld
2010-08-11 14:31:57
Click on a Button control, hold the mouse button down, move it over the form to see what I mean.
Hans Passant
2010-08-11 14:44:17
@Hans - I noticed that and updated my answer. It may or may not be a problem depending on what the OP is trying to accomplish..
Stuart Dunkeld
2010-08-11 14:59:49
Yup, this is exactly what the OP is asking about.
Hans Passant
2010-08-11 15:33:15
@Hans I suspect you're right but OP *could* for example be trying to 'lasso' a number of controls, in which case the button would be clicked on the form surface.
Stuart Dunkeld
2010-08-11 16:19:07