我已在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)
答案 0 :(得分:0)
将ContextMenu分配给 ListView 然后......
变化:
Me.ContextMenu = mnuContextMenu
要:
lv.ContextMenu = mnuContextMenu
如果不明显,Me
已更改为lv
。