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

有帮助吗?

解决方案

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.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top