문제

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