0

我试图弄清楚如何使用我添加到工具条中的项目。下面是我正在使用的代码,我在工具条中添加了几个项目“test1,test2”,但是如何选择它们并添加点击事件的代码?

谢谢。

Public Class MenuStripCustom
Inherits MenuStrip

Private WithEvents NavToolStrip As New ToolStripMenuItem("File")

Sub New()

    Dim tsi As New ToolStripMenuItem
    Dim tsi2 As New ToolStripDropDownButton
    Me.Items.Add(NavToolStrip)
    NavToolStrip.DropDownItems.Add("test1")
    NavToolStrip.DropDownItems.Add("test2")
End Sub

Private Sub NavToolStripDropDownButton_Click(sender As Object, e As EventArgs) Handles NavToolStrip.DropDownItemClicked
        ' What do I put here to handle different drop down items?
        ' ie. select case (dropDownItem)
        ' case: test1?
End Sub

结束类

4

1 回答 1

1

有多种方法可以将事件处理程序添加到 ToolStripMenuItem。您可以将 AddressOf 它传递给 ToolStripMenuItem 构造函数或使用 AddHandler 关键字。但是,将 ToolStripMenuItems 声明为“WithEvents”可能是最简单的,然后您可以在 VS IDE 中查看事件并将它们连接起来。例如:

Private WithEvents NavToolStrip As New ToolStripMenuItem("File")
Private WithEvents tsi As New ToolStripMenuItem("Test1")
Private WithEvents tsi2 As New ToolStripMenuItem("Test2")

Sub New()
    MenuStrip1.Items.Add(NavToolStrip)

    NavToolStrip.DropDownItems.Add(tsi)
    NavToolStrip.DropDownItems.Add(tsi2)

End Sub

Private Sub tsi_Click(sender As Object, e As EventArgs) Handles tsi.Click
    MsgBox("Test1")
End Sub

Private Sub tsi2_Click(sender As Object, e As EventArgs) Handles tsi2.Click
    MsgBox("Test2")
End Sub
于 2012-11-04T08:24:26.947 回答