0

This function only recognizes mouse's Left button. How can I make this program recognize mouse's right button in order to click this button with mouse's right button?.

 private void button2_Click(object sender, EventArgs e)
        {
            MouseEventArgs me = (MouseEventArgs)e;
            if (buttonwasclicked==false)
            {
                DrawLinesOnBitmap(button2.BackgroundImage);
                button2.BackgroundImage= ToGrayscale(button2.BackgroundImage);
                buttonwasclicked = true;
            }

            else {
                button2.BackgroundImageLayout = ImageLayout.Stretch; 
                button2.BackgroundImage = Image.FromFile("C:\\Users\\rati\\Desktop\\ks.png");
                buttonwasclicked = false;
            }
            if (me.Button == MouseButtons.Left)
            {
                mysum += md;
                if (buttonwasclicked == true) md *= -1; else md *= -1;
                label1.Text = mysum.ToString();
            }
            if (me.Button == MouseButtons.Right) {
                enemysum += ed;
                if (buttonwasclicked == true) ed *= -1; else ed *= -1;
                label2.Text = enemysum.ToString();
            }
        }
Ajay
  • 18,086
  • 12
  • 59
  • 105
Rati Sharabidze
  • 69
  • 1
  • 12
  • check this out ..http://stackoverflow.com/questions/3209217/detect-both-left-and-right-mouse-click-at-the-same-time you need to understand that the button click probably on fires when using the left mouse button.. so you need to follow the answer in the link I posted – MethodMan Oct 09 '15 at 19:39
  • Use the MouseClick event instead, test e.Button – Hans Passant Oct 09 '15 at 20:03
  • Would that be easier to use PreviewMouseDown event ? – cscmh99 Oct 09 '15 at 20:03

4 Answers4

0

Use the "MouseClick" event rather than the "Click" event, "Click" doesn't recognise right clicks of the mouse.

If you're using Visual Studio, simply go to the designer, click the button, go to properties and click the lightning icon. Then you find "MouseClick" and double click this.

Sophie Coyne
  • 1,018
  • 7
  • 16
0

You should use MouseDown event. It has MouseEventArgs parameter which contains Button property. So you can check wich button was down.

Paviel Kraskoŭski
  • 1,429
  • 9
  • 16
0

You need to use the MouseDown and MouseUp actions together in order to interpret a click.

Your actions list should look similar to this:

Control Properties

Then interpret the actions as follows:

int prevMouseX;
int prevMouseY;

private void mouseDown(object sender, MouseEventArgs e)
{
    prevMouseX = e.X;
    prevMouseY = e.Y;
}

private void mouseUp(object sender, MouseEventArgs e)
{
    if (prevMouseX == e.X && prevMouseY == e.Y)
        mouseClick(sender, e);
}

private void mouseClick(object sender, MouseEventArgs e)
{
    //Do Stuff
}

That should work for you!

My code interprets a click as when the mouse goes down and up in the same position.

Elipzer
  • 901
  • 6
  • 22
0

Try this if you want to do something on Button's right click event.

=> Create one context menu and do not create any option in it.(Drag & Drop ContextMenuStrip from Toolbox)

=> Assign that context menu to that button from Button's ContextMenuStrip property in Properties panel.

Write following code on Context Menu's Opening Event

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {
        MessageBox.Show("hii");
        e.Cancel=true;
    }
Mehul Patel
  • 1,084
  • 2
  • 12
  • 28
  • I need that button was clicked with mouse right click – Rati Sharabidze Oct 10 '15 at 05:30
  • obviously what ever code is written in this event will be executed by mouse right click only. – Mehul Patel Oct 10 '15 at 11:36
  • I manage to click with right,it writes "right",but the button can't be clicked that it took effect(I mean button is not clicked when I hit the right button of a mouse) – Rati Sharabidze Oct 10 '15 at 13:14
  • 1
    Whats the matter if button clicked or not? Button never click with right mouse button. It only click with left mouse button. What ever code you write in click event it always execute by left click only. If you want to execute some code by right click on button do it by this way otherwise there is no way to do this. – Mehul Patel Oct 10 '15 at 13:37
  • I tried every events on mouse but none of them worked. I think only mouse's left button can execute the button but theorically it must be possible. I know that I want to do a thing which is not classical but I want to do it – Rati Sharabidze Oct 10 '15 at 13:51