我从数据库中提取值并将其添加到子菜单中。我想知道如何在子菜单项上添加事件处理程序,以便如果某些正文单击显示为消息框(例如) 下面是代码,但它继续给我主菜单项而不是子菜单。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Sql Query for getting business locations
While reader.Read()
DatabaseToolStripMenuItem.DropDownItems.Add(reader(0)) 'adding business to menustrip main menu item (Databse)
End While
AddHandler MenuStrip1.Click, AddressOf ItemClicked
End Sub
Private Sub ItemClicked(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Test")
End Sub
问候
答案 0 :(得分:1)
如果您为MenuStrip1
添加事件处理程序,它将适用于MenuStrip1
点击
AddHandler MenuStrip1.Click, AddressOf ItemClicked
您需要为子菜单添加事件处理程序,
AddHandler SubMenu1ToolStripMenuItem.Click, AddressOf ItemClicked
AddHandler SubMenu2ToolStripMenuItem.Click, AddressOf ItemClicked
<强>更新:强>
While reader.Read()
Dim SubMenu As New ToolStripMenuItem
SubMenu.Text = reader(0)
AddHandler SubMenu.Click, AddressOf ItemClicked
DatabaseToolStripMenuItem.DropDownItems.Add(SubMenu)
End While
答案 1 :(得分:0)
用于将菜单项添加到其父项的Add方法已重载,您应该使用允许您同时指定Click事件处理程序的方法:
parentMenu.Items.Add(textToDisplay, imageToDisplay, AddressOf MethodToHandleClick)
或
parentMenuItem.DropDownItems.Add(textToDisplay, imageToDisplay, AddressOf MethodToHandleClick)
答案 2 :(得分:0)
这个youtube video概述了如何向菜单项添加事件处理程序。