Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether button was right or left clicked

Tags:

c#

winforms

my problem was as i believed simple but it turned to be a little more complicated, what i was trying to achieve was this: I have several buttons in my WinForm that do almost the same thing, so I've created a single event to handle all of them, now what i wished was to give them some other functionality based on which mouse button they clicked, for instance if the button is left clicked performs as it has been doing but if its right clicked performs in some other way, can this be achieved?, any help would be greatly appreciated.

I'm looking for some statement like this maybe...

 private void buttons_Click(object sender, EventArgs e)
         {
 if(e.buttons==mousebuttons.right)
 //do something 
 else
 //do other thing
 }

the thing as you know is that this can't be achieved since e has no mouse button events on it, cause i'ts being called as a button click event.

like image 478
user2793090 Avatar asked Feb 04 '26 17:02

user2793090


1 Answers

Just try with button1_MouseDown event instead of buttons_Click Event.It will solve your problem.

   private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
          //do something
        }
        if (e.Button == MouseButtons.Right)
        {
          //do something
        }
    }
like image 76
Thilina H Avatar answered Feb 06 '26 05:02

Thilina H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!