Included below are four methods and a Friend that can be inserted directly
into a form that already contains Button1, Button2, and MenuStrip1. Button1
populates the MainMenu control, button2 populates the MenuStrip control. The
MainMenu is from legacy 2003 and works fine. I want to upgrade to the
MenuStrip but cannot find a way to duplicate legacy code. Shown below is my
best effort. I am having two problems.
#1: ToolStripMenuItem appears not support the cascading of the MenuItems()
which successfully works for a MenuItem. If there is not a fix, this means
significant recoding.
#2: When I click the last item in the menu hierarchy, I want to read back
through the hierarchy of the clicked Menu entry but can't seem to get past
the parent of the clicked item. This code crashes when I attempt to get the
grandparent.
Any Help will be appreciated. There does not seem to be much on the
Internet or at Microsoft covering the MenuStrip control and the
GetCurrentParent() method or parent property.
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Private Sub ButtonMnu_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.Menu = Me.MainMenu1
Dim omnuAdd As MenuItem = New MenuItem("Menu Bar Level")
MainMenu1.MenuItems.Add(omnuAdd)
omnuAdd = New MenuItem("First Sub")
MainMenu1.MenuItems(0).MenuItems.Add(omnuAdd)
omnuAdd = New MenuItem("Second Sub")
MainMenu1.MenuItems(0).MenuItems(0).MenuItems.Add( omnuAdd)
omnuAdd = New MenuItem("Third Sub")
AddHandler omnuAdd.Click, AddressOf Me.mnuClick
MainMenu1.MenuItems(0).MenuItems(0).MenuItems(0).M enuItems.Add(omnuAdd)
End Sub
Private Sub mnuClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim oMnu As MenuItem = CType(sender, MenuItem)
Dim oMnuParent As MenuItem = oMnu.Parent
oMnuParent.MenuItems.Add("Added to Parent")
oMnuParent = oMnuParent.Parent
oMnuParent.MenuItems.Add("Added to GrandParent")
End Sub
Private Sub ButtonStrip_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim oStripParent As ToolStripMenuItem
Dim oStripAdd As New ToolStripMenuItem("Menu Strip Level")
Me.MenuStrip1.Items.Add(oStripAdd)
oStripParent = Me.MenuStrip1.Items(Me.MenuStrip1.Items.Count - 1)
oStripAdd = New ToolStripMenuItem("First Sub")
oStripParent.DropDownItems.Add(oStripAdd)
oStripParent = oStripParent.DropDownItems(oStripParent.DropDownIt ems.Count
- 1)
oStripAdd = (New ToolStripMenuItem("Second Sub"))
oStripParent.DropDownItems.Add(oStripAdd)
oStripParent = oStripParent.DropDownItems(oStripParent.DropDownIt ems.Count
- 1)
oStripAdd = (New ToolStripMenuItem("Third Sub"))
AddHandler oStripAdd.Click, AddressOf Me.stripClick
oStripParent.DropDownItems.Add(oStripAdd)
End Sub
Private Sub stripClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim oStrip As ToolStripMenuItem = CType(sender, ToolStripItem)
Dim oStripParent As ToolStrip = oStrip.GetCurrentParent
oStripParent.Items.Add("Added to Parent")
Dim oStripParent1 As ToolStrip = oStripParent.Parent
Dim oStripAdd As New ToolStripMenuItem("Added to GrandParent")
Try
oStripParent1.Items.Add(oStripAdd)
Catch ex As Exception
MsgBox("You crash here")
End Try
End Sub
Thank you