ToolStrip DropDown question

G

GatorBait

Hi all,

I'm using the new ToolStrip control in a project and I'm running into a
problem that I'm hoping someone can help me with. I am building the
toolstrip in code by adding buttons, seperators, and dropdown buttons
(which have MenuItems added to it). The ToolStrip gets built
perfectly, but the problem I am having is accessing the click event of
the dropdown button's menuitems.

I hope someone can help!!! Thank you so much in advance!

Below is the code I am using to create the toolstrip:

Private Sub ToolStrip1_ItemClicked(ByVal sender As Object, ByVal e
As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles
ToolStrip1.ItemClicked
Select Case e.ClickedItem.Text
Case "Help"
MsgBox("Help was clicked")
Case "Main Menu"
MsgBox("Main Menu was clicked")
Case "Print"
MsgBox("Print was clicked")
End Select
End Sub

Private Sub AddToolStripItem(Optional ByVal sBtnName As String =
"", Optional ByVal sBtnText As String = "", _
Optional ByVal sDropDownName As String
= "", Optional ByVal sDropDownText As String = "", _
Optional ByVal sMenuItemName As String
= "", Optional ByVal sMenuItemText As String = "")
Dim tsBtn As New ToolStripButton
Dim tsSep As New ToolStripSeparator
Dim tsDrop As New ToolStripDropDownButton
Dim tsMenu As New ToolStripMenuItem
Dim sTempName As String
Dim sTempText As String

tsSep.Name = "Seperator"

If sBtnName <> "" Then
tsBtn.DisplayStyle =
System.Windows.Forms.ToolStripItemDisplayStyle.Text
tsBtn.Name = sBtnName
tsBtn.Text = sBtnText

ToolStrip1.Items.Add(tsBtn)
ToolStrip1.Items.Add(tsSep)
ElseIf sDropDownName <> "" Then
tsDrop.DisplayStyle =
System.Windows.Forms.ToolStripItemDisplayStyle.Text
tsDrop.Name = "New Dropdown"
tsDrop.Text = "New Dropdown"
tsDrop.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft
ToolStrip1.Items.Add(tsDrop)
ToolStrip1.Items.Add(tsSep)

Do While sMenuItemName <> ""
tsMenu = New ToolStripMenuItem

If InStr(1, sMenuItemName, ",") > 0 Then
sTempName = Mid(sMenuItemName, 1, InStr(1,
sMenuItemName, ",") - 1)
sTempText = Mid(sMenuItemText, 1, InStr(1,
sMenuItemText, ",") - 1)
sMenuItemName = Mid(sMenuItemName, InStr(1,
sMenuItemName, ",") + 1)
sMenuItemText = Mid(sMenuItemText, InStr(1,
sMenuItemText, ",") + 1)
Else
sTempName = sMenuItemName
sTempText = sMenuItemText
sMenuItemName = ""
sMenuItemText = ""
End If

tsMenu.DisplayStyle =
System.Windows.Forms.ToolStripItemDisplayStyle.Text
tsMenu.Name = sTempName
tsMenu.Text = sTempText
tsMenu.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft
tsDrop.DropDownItems.Add(tsMenu)
Loop
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddToolStripItem("Help", "Help")
AddToolStripItem("MainMenu", "Main Menu")
AddToolStripItem(, , "AddPolicy", "Add A Policy",
"Item1,Item2,Item3", "Option1,Option2,Option3")
AddToolStripItem("Print", "Print")
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top