Question

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

Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top