0

我在 Windows 窗体应用程序中动态填充了一个列表。现在我想添加一个上下文菜单,这样当我选择一个项目时,右键单击所选项目时会出现上下文菜单,而右键单击表单的其他空白区域时不会出现上下文菜单。我目前正在使用此代码。

lv.Columns.Add("Button Text", 300, HorizontalAlignment.Left)
lv.Columns.Add("PID", 50, HorizontalAlignment.Left)
lv.Columns.Add("Process Path", 300, HorizontalAlignment.Left)
lv.Columns.Add("Hide Icon Permanently", 150, HorizontalAlignment.Left)

Dim things As List(Of TrayButton) = TrayHelper.Tray.GetTrayButtons()
For Each b As TrayButton In things
    If b.Icon IsNot Nothing Then
        il.Images.Add(b.TrayIndex.ToString, b.Icon)
    Else
        ' When we can't find an icon, the listview will display this form's one.
        ' You could try to grab the icon from the process path I suppose. 
        il.Images.Add(b.TrayIndex.ToString, Me.Icon)
    End If
    Dim lvi As New ListViewItem(b.Text)
    lvi.SubItems.Add(b.ProcessIdentifier.ToString)
    lvi.SubItems.Add(b.ProcessPath)
    lvi.ImageKey = b.TrayIndex.ToString
    lv.Items.Add(lvi)

Next

Dim mnuContextMenu As New ContextMenu()
Me.ContextMenu = mnuContextMenu
Dim mnuItemHide As New MenuItem()
mnuItemHide.Text = "&Hide"

mnuContextMenu.MenuItems.Add(mnuItemHide)
4

1 回答 1

0

将 ContextMenu 分配给ListView然后...

改变:

Me.ContextMenu = mnuContextMenu

至:

lv.ContextMenu = mnuContextMenu

如果不明显,Me则更改为lv.

于 2013-05-19T17:32:03.883 回答