开发者

ToolStripMenuItem not closing when a child ToolStripMenuItem is Clicked in a C# WinForm

开发者 https://www.devze.com 2022-12-20 21:04 出处:网络
Is there a way to have a ToolStripMenuItem not closing when I click a child control (in its DropDrowItems Collection)?

Is there a way to have a ToolStripMenuItem not closing when I click a child control (in its DropDrowItems Collection)?

In my case, I have some ToolStripMenuItems that work as a check box. Actually, I implemented a radio behavior in some ToolStripMenuItems using their Check property. But I don't want the menu closing when I click any of them, because they 开发者_如何转开发aren't an action, they represent just options in the menu item.

Is that possible?


   this.menuItem.DropDown.Closing += new ToolStripDropDownClosingEventHandler(DropDown_Closing); 

void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
            {
                if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
                {
                    e.Cancel = true;
                    ((ToolStripDropDownMenu) sender).Invalidate();
                } 
            }


Look for ToolStripDropDown.AutoClose property.


Just for your information:

  • The Closing event exists on the ContextMenuStrip and the ToolStripDropDown.
  • While designing with the designer, you have access to the ContextMenuStrip (the popup menu) and the ToolStripMenuItem (a submenu) objects, but not the ToolStripDropDown objects of the ToolStripMenuItem!
  • That's why you must set the Closing event of the ToolStripDropDown objects yourself by code (see Zabulus answer).
0

精彩评论

暂无评论...
验证码 换一张
取 消