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_Item Clicked(ByVal sender As Object, ByVal e
As System.Windows. Forms.ToolStrip ItemClickedEven tArgs) Handles
ToolStrip1.Item Clicked
Select Case e.ClickedItem.T ext
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 AddToolStripIte m(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 ToolStripSepara tor
Dim tsDrop As New ToolStripDropDo wnButton
Dim tsMenu As New ToolStripMenuIt em
Dim sTempName As String
Dim sTempText As String
tsSep.Name = "Seperator"
If sBtnName <> "" Then
tsBtn.DisplaySt yle =
System.Windows. Forms.ToolStrip ItemDisplayStyl e.Text
tsBtn.Name = sBtnName
tsBtn.Text = sBtnText
ToolStrip1.Item s.Add(tsBtn)
ToolStrip1.Item s.Add(tsSep)
ElseIf sDropDownName <> "" Then
tsDrop.DisplayS tyle =
System.Windows. Forms.ToolStrip ItemDisplayStyl e.Text
tsDrop.Name = "New Dropdown"
tsDrop.Text = "New Dropdown"
tsDrop.TextAlig n =
System.Drawing. ContentAlignmen t.MiddleLeft
ToolStrip1.Item s.Add(tsDrop)
ToolStrip1.Item s.Add(tsSep)
Do While sMenuItemName <> ""
tsMenu = New ToolStripMenuIt em
If InStr(1, sMenuItemName, ",") > 0 Then
sTempName = Mid(sMenuItemNa me, 1, InStr(1,
sMenuItemName, ",") - 1)
sTempText = Mid(sMenuItemTe xt, 1, InStr(1,
sMenuItemText, ",") - 1)
sMenuItemName = Mid(sMenuItemNa me, InStr(1,
sMenuItemName, ",") + 1)
sMenuItemText = Mid(sMenuItemTe xt, InStr(1,
sMenuItemText, ",") + 1)
Else
sTempName = sMenuItemName
sTempText = sMenuItemText
sMenuItemName = ""
sMenuItemText = ""
End If
tsMenu.DisplayS tyle =
System.Windows. Forms.ToolStrip ItemDisplayStyl e.Text
tsMenu.Name = sTempName
tsMenu.Text = sTempText
tsMenu.TextAlig n =
System.Drawing. ContentAlignmen t.MiddleLeft
tsDrop.DropDown Items.Add(tsMen u)
Loop
End If
End Sub
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
AddToolStripIte m("Help", "Help")
AddToolStripIte m("MainMenu", "Main Menu")
AddToolStripIte m(, , "AddPolicy" , "Add A Policy",
"Item1,Item2,It em3", "Option1,Option 2,Option3")
AddToolStripIte m("Print", "Print")
End Sub
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_Item Clicked(ByVal sender As Object, ByVal e
As System.Windows. Forms.ToolStrip ItemClickedEven tArgs) Handles
ToolStrip1.Item Clicked
Select Case e.ClickedItem.T ext
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 AddToolStripIte m(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 ToolStripSepara tor
Dim tsDrop As New ToolStripDropDo wnButton
Dim tsMenu As New ToolStripMenuIt em
Dim sTempName As String
Dim sTempText As String
tsSep.Name = "Seperator"
If sBtnName <> "" Then
tsBtn.DisplaySt yle =
System.Windows. Forms.ToolStrip ItemDisplayStyl e.Text
tsBtn.Name = sBtnName
tsBtn.Text = sBtnText
ToolStrip1.Item s.Add(tsBtn)
ToolStrip1.Item s.Add(tsSep)
ElseIf sDropDownName <> "" Then
tsDrop.DisplayS tyle =
System.Windows. Forms.ToolStrip ItemDisplayStyl e.Text
tsDrop.Name = "New Dropdown"
tsDrop.Text = "New Dropdown"
tsDrop.TextAlig n =
System.Drawing. ContentAlignmen t.MiddleLeft
ToolStrip1.Item s.Add(tsDrop)
ToolStrip1.Item s.Add(tsSep)
Do While sMenuItemName <> ""
tsMenu = New ToolStripMenuIt em
If InStr(1, sMenuItemName, ",") > 0 Then
sTempName = Mid(sMenuItemNa me, 1, InStr(1,
sMenuItemName, ",") - 1)
sTempText = Mid(sMenuItemTe xt, 1, InStr(1,
sMenuItemText, ",") - 1)
sMenuItemName = Mid(sMenuItemNa me, InStr(1,
sMenuItemName, ",") + 1)
sMenuItemText = Mid(sMenuItemTe xt, InStr(1,
sMenuItemText, ",") + 1)
Else
sTempName = sMenuItemName
sTempText = sMenuItemText
sMenuItemName = ""
sMenuItemText = ""
End If
tsMenu.DisplayS tyle =
System.Windows. Forms.ToolStrip ItemDisplayStyl e.Text
tsMenu.Name = sTempName
tsMenu.Text = sTempText
tsMenu.TextAlig n =
System.Drawing. ContentAlignmen t.MiddleLeft
tsDrop.DropDown Items.Add(tsMen u)
Loop
End If
End Sub
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
AddToolStripIte m("Help", "Help")
AddToolStripIte m("MainMenu", "Main Menu")
AddToolStripIte m(, , "AddPolicy" , "Add A Policy",
"Item1,Item2,It em3", "Option1,Option 2,Option3")
AddToolStripIte m("Print", "Print")
End Sub
Comment