右键单击时 ContextMenuStrip,如果在列表视图中选择了项目?

发布于 2024-09-16 03:05:46 字数 88 浏览 4 评论 0原文

我有一个附加到列表视图的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

困倦 2024-09-23 03:05:46

您可以使用打开事件。事件参数具有 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 setting e.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.

月下伊人醉 2024-09-23 03:05:46

对于阅读此帖子的其他人来说,一个不错的方法是在未选择任何项目时将菜单中的选项(在打开事件中)灰显,而不是根本不显示菜单

if (List.SelectedItems.Count == 0)
{
    // e.Cancel=true;
    List.Enabled = false;
}
else
{
    List.Enabled = true;
}

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

if (List.SelectedItems.Count == 0)
{
    // e.Cancel=true;
    List.Enabled = false;
}
else
{
    List.Enabled = true;
}
栀子花开つ 2024-09-23 03:05:46

对我来说,直观的是,如果您没有选择任何项目(或者右键单击未选择的项目),则在显示上下文菜单之前会自动选择该项目。

如果第一个解决方案不可接受,我想我会尝试在选择项目时附加 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.

緦唸λ蓇 2024-09-23 03:05:46
   Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
        If e.Button = MouseButtons.Right And ListView1.SelectedItems.Count > 0 Then
            Dim cn As New ContextMenuStrip()
            cn.Items.Add("Apple")
            Me.ListView1.ContextMenuStrip = cn
            cn.Show(Control.MousePosition.X, Control.MousePosition.Y)
        End If
    End Sub
   Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
        If e.Button = MouseButtons.Right And ListView1.SelectedItems.Count > 0 Then
            Dim cn As New ContextMenuStrip()
            cn.Items.Add("Apple")
            Me.ListView1.ContextMenuStrip = cn
            cn.Show(Control.MousePosition.X, Control.MousePosition.Y)
        End If
    End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文