0

If I have a context menu with sub menu items, is it possible to stop the sub menu from popping out/displaying when I merely hover over the main menu item? And if so, how?

Submenu item example screenshot

MrVimes
  • 3,212
  • 10
  • 39
  • 57

1 Answers1

2

Each ToolStripDropDownItem has a property called DropDown (of type ToolStripDropDown) referring to the drop down which will be shown when the mouse hovers on the item. The ToolStripDropDown has an event called Opening which allows you to cancel the dropping-down easily. Use the following code, all can be set up in your form constructor:

//Suppose the item you want to suppress automatically showing 
//the drop down is item1
bool clicked = false;
item1.DropDown.Opening += (s,e) => {
  e.Cancel = !clicked;
  clicked = false;
};
item1.Click += (s,e) => {
  clicked = true;
  item1.ShowDropDown();
};
//The code above disables the automatic dropping-down 
//and shows the drop down by clicking on the item1.
King King
  • 61,710
  • 16
  • 105
  • 130
  • Works perfectly, Thankyou! I notice some syntax I've never seen before. I'm new to c#. I guess that's a way to override methods c#? I will google that to learn more. – MrVimes Oct 27 '13 at 09:55
  • @MrVimes the syntax I used is called `lambda expression`, it's just a convenient way for building `delegate` easily, the code is just used to register the `Opening` and `Click` event handlers. It's not `override`, to override you have to create some class inherits from the base class and override the base methods in there. – King King Oct 27 '13 at 12:47