右键单击时 ContextMenuStrip,如果在列表视图中选择了项目?
我有一个附加到列表视图的 ContextMenuStrip,它工作得很好,但我感兴趣的是如何让它仅在列表视图中选择一个或多个项目时才显示。
谢谢!
I have a ContextMenuStrip attached to a list view, and it's working great, but what I'm interested in knowing is how I can have it show up only when one or more items are selected in the listview.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
打开
事件。事件参数具有 Cancel 属性,以便您可以检查应用程序的状态并决定是否显示菜单(通过不执行任何操作)或阻止其显示(通过设置
e.Cancel = true
)。然而,就像 @Grzenio 提到的那样,如果我右键单击的项目自动被选中,我会发现它更直观。另一种选择是使用
Opening
事件仅使用一个禁用的项目填充上下文菜单,并使用类似(未选择任何项目)
等文本;这将通知用户该命令不可用的原因。You could use the
Opening
event. The event args has a Cancel property so that you can examine the state of your application and decide whether to have the menu show (by doing nothing) or prevent it from showing (by settinge.Cancel = true
). However, like @Grzenio mentions, I would find it more intuitive if the item that i right-clicked on became selected automatically.Another option would be to use the
Opening
event to populate the context menu with only one disabled item, with a text like(no item is selected)
or so; this would inform the user about why the command is not available.对于阅读此帖子的其他人来说,一个不错的方法是在未选择任何项目时将菜单中的选项(在打开事件中)灰显,而不是根本不显示菜单
For other people reading this thread, a nice way is to gray out the options in the menu (in Opening event) when no items are selected instead of not displaying the menu at all
对我来说,直观的是,如果您没有选择任何项目(或者右键单击未选择的项目),则在显示上下文菜单之前会自动选择该项目。
如果第一个解决方案不可接受,我想我会尝试在选择项目时附加 ContextMenuStrip,并在取消选择项目时将其分离。
For me its intuitive that if you have no items selected (or you right-click on a non-selected item), the item would get automatically selected just before you show the context menu.
If the first solution is not acceptable, I think I would try to attach the ContextMenuStrip when items get selected and detach it when they are unselected.