0

I want to capture the right click mouse event within the ToolStripDropDownItemClicked Event

private void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)

Based on the right click, i display a contextmenu. So, I need to get both the item clicked and mouse click right.

Jameer Basha
  • 61
  • 1
  • 9
  • Do you need to know the mouse position or what button was clicked? The event can't be cast to a MouseEventArgs object, so I don't know that this is possible. – MrFox Aug 20 '15 at 12:14
  • I need to get the Clicked DropDown item and the right mouse click or not – Jameer Basha Aug 20 '15 at 13:59

1 Answers1

0

You could use the MouseUp or MouseDown event it knows which button was pressed. The sender object is the menu item that was clicked. Cast it to the correct type to access it's properties. If you're not sure about the type you could use as and then check if the resulting object is null.

void firstToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
{
    var toolStripMenuItem = (ToolStripMenuItem)sender;
    var nullIfOtherType = sender as ToolStripMenuItem;
    var right = e.Button == System.Windows.Forms.MouseButtons.Right;
}

How to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?

Community
  • 1
  • 1
MrFox
  • 4,852
  • 7
  • 45
  • 81
  • I am getting an exception "Unable to cast object of type System.Windows.Forms.ToolStipDropDownButton to type System.Windows.Forms.ToolStripMenuItem – Jameer Basha Aug 21 '15 at 05:52
  • Then apparently your sender object is a 'ToolStripDownButton' so that's what you should cast to. The casting only allows you to use the more specific interface that the object already has, it does not change the object in any way. So in my example I had a menu item, causing the framework to send me a menu item in the event, so that's what I cast to. If you are using a button, you need to cast to a button so you can use it's full interface. – MrFox Aug 21 '15 at 08:40