Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# mouseleave and mouseenter event wont fire if the mouse button is clicked

The titles says it all. I have a panel that acts as a white board. On mouse move draw the mouse track.. works fine, but if the mouse leaves the edges of the panel, i want to call the mouse up event and mouse down event if the the mouse leaves or enters the panel while the left button is clicked

 private void panel2_MouseLeave(object sender, EventArgs e)
    {
        if (mousedraw == true)
        {
            panel2_MouseUp(sender, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 0, MousePosition.X, MousePosition.Y, 0));
        }
    }

    private void panel2_MouseEnter(object sender, EventArgs e)
    {
        if (mousedraw == true)
        {
            panel2_MouseDown(sender, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 0, MousePosition.X, MousePosition.Y, 0));
        }
    }

mousedraw is a bool to know if the left button is clicked.

The problem is:

The leave and enter events will not fire if the mouse button is down.

like image 788
Osama Avatar asked Aug 27 '11 23:08

Osama


1 Answers

MouseEnter and mouseLeave do not fire while a button is pressed. However, when the button is eventually released, the appropriate mouseEnter or mouseLeave event fires if the mouse has moved in or out of the panel while the button was down. As long as the button is pressed, the mouseMove event will continue to fire, even outside the bounds of the panel. This allows the mouse to continue dragging or whatever even after it passes outside the control's boundary, and is the way most Windows applications work.

If you can use this behavior in your application, it will be a more "standard" user interface.

If you definitely need to fire a mouseUp when the mouse leaves the panel, you can check the mouse location in the mouseMove event and call mouseUp whenever it is outside the panel and the button is pressed. In the MouseMove handler you can use e.X and e.Y for the location, and e.Button for the button status.

When the mousebutton is pressed outside the control and moved inside, then the panel has no jurisdiction over the mouse, because the mouse is considered to be moving in the form or whatever control it was in when the button was pressed. So you might have trouble getting mouseDown to fire when the mouse button is pressed outside the panel and then moved inside the panel.

like image 189
xpda Avatar answered Oct 13 '22 00:10

xpda