简体   繁体   English

在C#WinForm中单击子ToolStripMenuItem时,ToolStripMenuItem未关闭

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

Is there a way to have a ToolStripMenuItem not closing when I click a child control (in its DropDrowItems Collection)? 当我单击子控件(在其DropDrowItems集合中)时,有没有办法使ToolStripMenuItem不关闭?

In my case, I have some ToolStripMenuItems that work as a check box. 在我的例子中,我有一些ToolStripMenuItems作为一个复选框。 Actually, I implemented a radio behavior in some ToolStripMenuItems using their Check property. 实际上,我使用Check属性在一些ToolStripMenuItems中实现了一个无线电行为。 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();
                } 
            }

Just for your information: 仅供参考:

  • The Closing event exists on the ContextMenuStrip and the ToolStripDropDown . Closing事件存在于ContextMenuStripToolStripDropDown上
  • 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! 在使用设计器进行设计时,您可以访问ContextMenuStrip (弹出菜单)和ToolStripMenuItem(子菜单)对象,但不能访问ToolStripMenuItem的ToolStripDropDown对象!
  • That's why you must set the Closing event of the ToolStripDropDown objects yourself by code (see Zabulus answer). 这就是为什么你必须通过代码自己设置ToolStripDropDown对象的Closing事件(参见Zabulus答案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM