选择某些项目时不要关闭 ContextMenuStrip
选择/检查某些项目后是否可以使 ContextMenuStrip 保持打开状态?
我计划使用一个简单的 ContextMenuStrip 来设置过滤器(这样我可以在菜单中或作为右键单击选项使用相同的过滤器)。
菜单列出了许多项目,我希望用户能够使用基本的检查功能来选择项目。 选择完成后,用户可以单击“激活过滤器”选项,也可以单击菜单外部来激活或取消过滤器。
在选择/单击事件时,菜单通常会关闭。 是否可以在单击事件中保持菜单打开?
Is it possible to leave a ContextMenuStrip open after a selection/check of certain items?
I plan on using a simple ContextMenuStrip to set a filter (this way i could use the same filter either in a menu or as a right-click option).
The menu lists a number of items, and i would like the user to be able to make a selection of the items using the basic Check functionality. Once the selection is done the user can click an Activate filter option or can click outside the menu to either activate or cancel the filter.
On a selection/click event the menu normally closes.
Is it possible to keep the menu open on a click event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
关闭事件
设置 e.Cancel = true 以使菜单保持打开状态,
唯一的问题是该事件不会告诉您单击了什么,因此您必须自己跟踪这一点。 在要保持菜单打开的项目的 Click 事件中设置某种标志。 然后在 Closing 事件中检查标志并适当设置 e.Cancel。
the Closing event
set e.Cancel = true to leave the menu open
only problem is the event doesn't tell you what was clicked, so you have to keep track of this yourself. set some kind of flag in the Click event of the items you want to keep the menu open. then in the Closing event check the flag and set e.Cancel appropriately.
要防止单击某项时上下文菜单关闭,请执行以下操作。
在 ContextMenuItems 的 mousedown 事件中,将标志设置为 false,然后在上下文菜单的关闭事件中将其设置回 true。
例子:
To prevent the contextmenu from closing when an item is clicked, do the following.
On mousedown event of ContextMenuItems set flag to false then set it back to true at the closing event of the contextmenu.
Example:
我发现奇怪的是
ContextMenuStrip.Closing
事件在ToolStripMenuItem.Click
事件之前触发。 解决方案是使用e.ClickedItem
处的ContextMenuStrip.ItemClicked
事件,然后检查它是否是单击时不会关闭 < code>ContextMenuStrip,并设置适当的标志。 然后,如果还设置了该标志,则可以在 ContextMenuStrip.Closing 中设置 e.Cancel = true; 。 但不要忘记重置标志。What i found strange is that
ContextMenuStrip.Closing
event fires before theToolStripMenuItem.Click
event. The solution was to useContextMenuStrip.ItemClicked
event where you havee.ClickedItem
, and then check if it's one of the items that, when clicked, won't close theContextMenuStrip
, and set the appropriate flag. Then inContextMenuStrip.Closing
you can sete.Cancel = true;
if the flag is also set. Don't forget to reset the flag though.我认为 ContextMenuStrip 中没有为此的属性。
我们在应用程序中使用的解决方法是,在 ContextMenuStrip 的单击事件上,我们进行一些处理,然后如果我们希望上下文菜单保持打开状态,我们只需再次调用 ContextMenuStrip.Show 即可。
如果 ContextMenuStrip 只有一级,则此方法效果很好。 如果有子菜单和子子菜单,那么您必须重新选择单击之前打开的菜单,我不确定如何做到这一点......
I don't think there is a property for this in the ContextMenuStrip.
The workaround we use in our application is that on the clicked event of the ContextMenuStrip, we do some processing, then if we want the context menu to stay open we simply call ContextMenuStrip.Show again.
This will work well if there is only one level to the ContextMenuStrip. If there are sub-menus and sub-sub-menus, then you would have to re-select the menus that were open before the click and I'm not sure how that can be done...
OnClosing,执行: e.Cancel = e.CloseReason != ToolStripDropDownCloseReason.CloseCalled;
然后当您决定关闭时,调用 Close()。
OnClosing, do: e.Cancel = e.CloseReason != ToolStripDropDownCloseReason.CloseCalled;
and then when you decide to close, call Close().
这是我的方法; 它不闪烁,而且——我认为——更灵活。
如果您有一组 ToolStripMenuItems 想要用作切换按钮(选项开/关),请尝试以下操作:(
ctxWildCards
只是我的ContextMenuStrip
,用于根据文件类型选择过滤器 - 用于搜索或文件对话框)这是在 Visual Basic 中(显然!;),因此您可以以编程方式或使用“处理...”子句添加处理程序。
This is my method; it's flicker-free and - I think - a bit more flexible.
If you have a set of ToolStripMenuItems you'd like to use as toggle buttons (option on/off), try this:
(The
ctxWildCards
is just myContextMenuStrip
, used to select filters based on file types - for search or FileDialogs)This is in Visual Basic (obviously! ;), so you can add Handlers programmatically or using 'Handles...' clauses.
我发现在不闪烁的情况下执行此操作的最佳方法是对 DropDown 菜单中的每个按钮使用 MouseDown 和 MouseLeave 事件。
例子:
The best way I found to do this without flickering is to use the MouseDown and MouseLeave events for every button in the DropDown menu.
Example:
我发现这对我的目的很有用。
您可以使用
ItemClicked
来读取标签或其他一些属性。我只是想要一个简单的项目,让用户清楚上下文菜单将影响哪个项目。
I found this useful for my purposes.
You could use
ItemClicked
to read the tag or some other property.I just wanted a simple item that made clear to the user which item the context menu was going to effect.
如果未来的程序员想知道如何做到这一点,这就是我的想法。 如果单击任何项目,这不会关闭上下文菜单。 创建上下文菜单条关闭事件并设置 if 语句,以在关闭原因为 itemclicked 时取消关闭事件。
In case future programers are wondering how to do this, this is what I figured out. This will not close the context menu if any item is clicked. Create the context menu strip closing event and setup an if statement to cancel the close event if close reason is itemclicked.