Question

I have a program where I want a right click on a button to do a completely different amount of code. I have the code display a messagebox for the example, but eventually it will just be a method call each. I'll show you the context in which I need it. Any and all help on how to detect a right click will help. Here's the snippet of code I have:

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             String^ buttonName = safe_cast<Button^>(sender)->Name;
             safe_cast<Button^>(sender)->Enabled = false;
             if(/*Insert Code Here To Detect a right click*/)
                 MessageBox::Show("Right Click");
             else
                 MessageBox::Show("Left Click");
             MessageBox::Show(buttonName);
         }
};
Was it helpful?

Solution

You can use the MouseDown event and check if it is right

void button1_MouseDown( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
      {
         // Update the mouse path with the mouse information
         Point mouseDownLocation = Point(e->X,e->Y);
         String^ eventString = nullptr;
         switch ( e->Button )
         {
            case ::MouseButtons::Left:
               eventString = "L";
               break;

            case ::MouseButtons::Right:
               eventString = "R";
               break;

            case ::MouseButtons::Middle:
               eventString = "M";
               break;

            case ::MouseButtons::XButton1:
               eventString = "X1";
               break;

            case ::MouseButtons::XButton2:
               eventString = "X2";
               break;

            case ::MouseButtons::None:
            default:
               break;
         }
        //Process Here...
      }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top