views:

54

answers:

2

I have this problem that event called "MouseEnter" does not fire when mouse button is held down. How could i fix it?

A: 

HI,

MouseEnter is the event that gets fired when the pointer enters the component, not when a mouse button is pressed. You 'll want another event: mousedown or something similar

grtz

Edit: I didn't read the question correctly... try this solution: Forum

Stephane
+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
There won't be any MouseMove messages either.
Hans Passant
@Hans - yes there are, I tested it worked..
Stuart Dunkeld
Click on a Button control, hold the mouse button down, move it over the form to see what I mean.
Hans Passant
@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
Yup, this is exactly what the OP is asking about.
Hans Passant
@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