我有Windows Form App。我添加了当您在列表框上单击鼠标右键时出现的上下文菜单。但有些原因我无法弄清楚如何捕获ContextMenu- Menu-Item的onClick事件。在用户点击菜单项时,还需要从列表框中获取项目索引。如果我点击鼠标左键我将使用lstCustomer.SelectedItems(0).SubItems(2).Text,但是当用户右键点击该项目并选择菜单项时,我不确定它是否相同从上下文菜单。
Public Class Dashboard
Private Sub Dashboard_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadContextMenu()
End Sub
Private Sub LoadContextMenu()
Dim contxMnu As New ContextMenu()
Dim menuItem1 As New MenuItem()
Dim menuItem2 As New MenuItem()
contxMnu.MenuItems.AddRange(New MenuItem() {menuItem1, menuItem2})
menuItem1.Index = 0
menuItem1.Text = "Do Something 1"
menuItem2.Index = 1
menuItem2.Text = "Do Something 2"
Me.ContextMenu = contxMnu
End Sub
End Class
答案 0 :(得分:3)
我不确定我是否完全明白你想要得到什么。如果它是关于处理Click事件的动作的全部,你可以这样做:
Private Sub LoadContextMenu()
Dim contxMnu As New ContextMenu()
Dim menuItem1 As New MenuItem("Do Something 1", New EventHandler(AddressOf DoSomething1))
Dim menuItem2 As New MenuItem("Do Something 2", New EventHandler(AddressOf DoSomething2))
contxMnu.MenuItems.AddRange(New MenuItem() {menuItem1, menuItem2})
Me.ContextMenu = contxMnu
End Sub
......事件是:
Private Sub DoSomething1(sender As Object, e As EventArgs)
' do something 1
End Sub
Private Sub DoSomething2(sender As Object, e As EventArgs)
' do something 2
End Sub