3

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.

Display Name
  • 8,022
  • 3
  • 31
  • 66
user2793090
  • 183
  • 2
  • 2
  • 10
  • 1
    A "click" by definition is a single, left mouse click. Your user will expect this, too. It might be confusing if something happens on right-clicking a button in your program. Maybe you can find a more standard conforming way to have your actions started. After all a button only has a single text on it, it's a bit hard to convey two meanings of what will happen for the two ways to click the button to your user with only a single text. – nvoigt Oct 04 '13 at 05:40
  • Anytime that I have done something like this I have used the MouseUp Event since the click event consists of both a MouseDown and a MouseUp event and doesn't get fired until the Mouse is released so it is in my mind the most logical place to do something like that. – Mark Hall Oct 04 '13 at 05:53
  • 1
    You can _type cast_ `EventArgs e` to `MouseEventArgs`. However, it will only catch `mousebuttons.left` but not `mousebuttons.right`. – Edper Oct 04 '13 at 05:54

5 Answers5

8

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
        }
    }
Thilina H
  • 5,754
  • 6
  • 26
  • 56
3

It doesn't seem to work with the click event. But the MouseDown event will give you a MouseEventArgs that has a button property. So perhaps you can modify your code to use MouseDown istead of click?

var btn = new Button();
btn.Text = "BUTTON";
btn.MouseDown += ((o, e) => {
    MessageBox.Show(e.Button.ToString());
});
Smetad Anarkist
  • 898
  • 9
  • 19
  • 2
    Since a click event consists of a MouseDown and a MouseUp I would be more inclined to use the MouseUp Event instead. IMHO – Mark Hall Oct 04 '13 at 05:47
2

Create a class boolean variable for the left and right button defaulted to false. When the mouse down event fires set the variable to true and check if both are true. When the mouse up fires set the variable to false.

public bool m_right = false;
public bool m_left = false;

private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
    m_objGraphics.Clear(SystemColors.Control);

    if (e.Button == MouseButtons.Left)
        m_left = true;
    if (e.Button == MouseButtons.Right)
        m_right = true;

    if (m_left == false || m_right == false) return;
    //do something here
}

private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
        m_left = false;
    if (e.Button == MouseButtons.Right)
        m_right = false;
 }

Source

Community
  • 1
  • 1
0

The following code worked nicely for me. (Note that I am using an EventArgs type similar to the function in the question).

private void buttons_Click(object sender, EventArgs e) {

Type t = e.GetType();
if (t.Equals(typeof(MouseEventArgs)))
{
    MouseEventArgs mouse = (MouseEventArgs)e;

    if (mouse.Button == MouseButtons.Left)
    {
        Console.WriteLine("****Left!!");
    }

    if (mouse.Button == MouseButtons.Right)
    {
        Console.WriteLine("****Right!!");
    }

}

}

0

Objective: To handle both mouse Left-Click and mouse Right-Click.

Problem: The Click event is not raised when the Mouse Right-Button is clicked. It is only raised when the Mouse Left-Button is depressed then released.

Solution:

  1. Subscribe to both the Click, and the MouseRightButtonUp events.

Note A. The MouseRightButtonUp event is raised when the Mouse Right-Button is released over the button, irrespective of whether it was over the button when it was initially pressed.

Note B. It is not relevant to this situation, but you should note that the Click event of a Button object (pressing and releasing the Mouse Left-button) handles both the MouseLeftButtonDown and the MouseLeftButtonUp events, and therefore neither of those events can be handled when you use the mouse Left-button to click a Button object. If you do have a need to handle those actions separately you should subscribe to PreviewMouseLeftButtonDown, and/or PreviewMouseLeftButtonUp.

  1. You can then either:

    a. have 2 separate event handlers, one for each of these events, or

    b. write an event handler that can handle both the Click and the MouseRightButtonUp events. Note: A Click event needs a handler which accepts an argument of type RoutedEventArgs, whereas the MouseRightButtonUp event requires one of type MouseButtonEventArgs. Both RoutedEventArgs and MouseButtonEventArgs inherit from EventArgs. Therefore, make the handler accept EventArgs. The handler should then determine whether it has been passed a RoutedEventArgs (in which case it performs the actions required for a Left-click), or a MouseButtonEventArgs (in which case it performs the actions required for a Right-click).

Code example:

myButton.MouseRightButtonUp += MenuItemButton_Click;
myButton.Click += MenuItemButton_Click;

internal void MenuItemButton_Click(object sender, EventArgs e)
{
    var clickedButton = sender as Button;
    string EventArgsType = e.GetType().ToString();
    //Remove leading qualification from Type ("System.Windows." or "System.Windows.Input.")
    EventArgsType = type.Substring(EventArgsType.LastIndexOf('.') + 1);
    switch (EventArgsType)
    {
        //Handle R-Click (RightMouseButtonUp)
        case "MouseButtonEventArgs":
            var MbeArgs = e as MouseButtonEventArgs;
           //Do stuff
            break;
        //Handle L-Click (Click)
        case "RoutedEventArgs":
            var ReArgs = e as RoutedEventArgs;
            //Do stuff
            break;
        default:
            break;
    }
}
Dr Bob
  • 1
  • 2