6

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.

1
  • This is a side-effect to the Control base class setting the Capture property to true on the mouse down event. This does not need fixing, drawing outside of the control Bounds doesn't show up anyway. Aug 29, 2011 at 2:35

2 Answers 2

7

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.

1
  • thank you, that was informative and helpful, i see now that the solution im seeking is not as simple as i thought it would, if (when) i handle this i will post the solution here for others to comment and benefit from.
    – Osama
    Aug 28, 2011 at 19:58
0

I donnt think [mousedraw] could do well

click = mouseDown + mouseUp 

so ,

When mouseDown :  mousedraw = false;

When mouseUp :    mousedraw = true;
2
  • maybe i didnt explain enough, or i dont understand you. [mousedraw] is a bool that gets changed whenever a mouse down or mouse up events is hit, so in mouse down i set mousedraw to true, and on mouse up i set it to false.
    – Osama
    Aug 28, 2011 at 19:51
  • I don't get how this should be used, maybe you should add some explanation
    – Breeze
    Apr 29, 2016 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.