Meowbits
Meowbits

Reputation: 586

ToolStripDropDownItem - how to use correctly? vb.Net

I am trying to figure out how to use the items I have added to my toolstrip. Below is the code I am using, I have addeda couple items "test1, test2" to the toolstrip, but how do I select them and add the code for on click events?

Thanks.

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

End Class

Upvotes: 0

Views: 2715

Answers (1)

sj1900
sj1900

Reputation: 412

there are a number of ways to add event handlers to a ToolStripMenuItem. You can pass the AddressOf it to the ToolStripMenuItem constructor or use the AddHandler keyword. However, it might be easiest for you to declare the ToolStripMenuItems "WithEvents", then you can see the events and wire them up in the VS IDE. E.g.:

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

Upvotes: 1

Related Questions