0

I need to catch the MouseUp on right click anywhere on the form. I understand how to create custom event to handle the right click event on individual controls, but having issues with catching it regardless of where the right click happens; over a control or even in a blank space on the form.

So as stated, I understand how to catch the right click per individual controls:

this.myButton.MouseUp += new MouseEventHandler(this.myButton_MouseUpToGetHelpText);


private void myButton_MouseUpToGetHelpText(object sender, MouseEventArgs e)
{
      if (e.Button == System.Windows.Forms.MouseButtons.Right)
       {
          // Logic here
       }
}

Just not quite sure how to listen for an event on the whole form. Any thoughts/suggestions appreciated.

Jurgen Camilleri
  • 3,559
  • 20
  • 45
Tony D.
  • 551
  • 1
  • 10
  • 26

1 Answers1

0

Simple:

this.MouseUp += new MouseEventHandler(this.form_MouseUpToGetHelpText);


private void form_MouseUpToGetHelpText(object sender, MouseEventArgs e)
{
  if (e.Button == System.Windows.Forms.MouseButtons.Right)
  {
      // Logic here
  }
}
Jurgen Camilleri
  • 3,559
  • 20
  • 45
  • Welp, that was easy. Thanks Jurgen. Curious, this handles the right click anywhere in a blank space on the form, but is there a to also include the controls? What I mean is anywhere a right click occurs this event needs to fire. Regardless if the mouse is over a control or blank space. – Tony D. May 06 '15 at 15:16
  • 1
    Unfortunately there's no easy way to do that - except maybe adapting this solution to your scenario http://stackoverflow.com/a/248017/1245065. Usage would be like so: `this.initControlsRecursive(this.Controls);`. Remember to change `MouseClick` to `MouseUp`! :) – Jurgen Camilleri May 06 '15 at 15:25
  • Glad I could be of help. – Jurgen Camilleri May 06 '15 at 16:23