0

I am experiencing an odd behaviour of the Capture property in a Form. I am setting it to true and subscribing to the mouse click event in the form to check if the event is being fired every time the mouse is clicked inside the client area of the form. In a simple test with a form and three buttons I am seeing that the capture works well only on the first attempt, and the event handler is not called afterwards. Here is the code:

public partial class Form1 : Form
{
    public Form1()
    {
        //Form1 has three buttons that are created inside InitializeComponent. It also subscribes Form1_MouseClick handler to the MouseClick event of the Form
        InitializeComponent();
        // Setting Capture to true will force to handle any mouse click on any control in the form
        Capture = true;
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        // This message box should appear after any click in the form or control inside it
        MessageBox.Show("Click!");
    }
}

I have also tried subscribing to MouseCaptureChanged event to reenable Capture property to true, but with no success. Could anyone explain to me the reason of this behaviour?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jose Antonio
  • 451
  • 2
  • 7
  • 24
  • Try using the [Mouse Down](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousedown(v=vs.110).aspx) event instead. Also, you don't need to set `Capture=True`. I managed to create this project and it worked perfectly when I tested it. – Lucax May 25 '18 at 14:28
  • 2
    That's not what mouse capture is for. Create a regular button. Don't do anything, anything at all with capture anywhere at all. Now click on the button. While the mouse button is still pressed, without releasing it at all, drag the mouse out of the button and back onto the button, *then* release it. That's what mouse capture is for: The button captures mouse input when you mouse down on the button, and releases mouse input when you mouse up. – 15ee8f99-57ff-4f92-890c-b56153 May 25 '18 at 14:32
  • @Lucax: Have you tried adding some buttons to the form and checking if MouseDown handler is fired in the form when you click on any button? In my test project the MouseDown event is not fired when clicking in the buttons – Jose Antonio May 25 '18 at 14:40

0 Answers0