0

I have a button in my Windows Forms app and I was trying to listen for a right click but when i tried to check for MouseButtons.Right it does not work. But MouseButtons.Left works

My code:

exitButton.MouseClick += (sender, e) =>
    {
        if (e.Button == MouseButtons.Right)
        {
            Application.Exit();
        }
    };

it works with Left but not right

  • Is this `Button` created at run time? If not, is there a particular reason that you're using a lambda instead of a named event handler? – John Aug 11 '22 at 05:39
  • In my testing, it seems like a `Button` control doesn't raise either a `Click` or `MouseClick` event on a right click. Not sure why that is but, given the intention behind the `Button` control, performing an action on a right-click other than displaying a menu seems counter-intuitive anyway. That intention is probably the explanation for the lack of events. – John Aug 11 '22 at 05:47
  • Specific to the Button class, it only raises the MouseClick event on a left click. You'd have to use the MouseUp event to detect such a click. But keep in mind that you'll have to write a manual, your user will never think that right-clicking a button does anything useful. Otherwise discovered only by accident, Application.Exit() is then especially unlikely to be appreciated. – Hans Passant Aug 11 '22 at 13:21

0 Answers0