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: ToolStripMenuIt em 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
GetCurrentParen t() 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.EventArg s) Handles Button1.Click
Me.MainMenu1 = New System.Windows. Forms.MainMenu
Me.Menu = Me.MainMenu1
Dim omnuAdd As MenuItem = New MenuItem("Menu Bar Level")
MainMenu1.MenuI tems.Add(omnuAd d)
omnuAdd = New MenuItem("First Sub")
MainMenu1.MenuI tems(0).MenuIte ms.Add(omnuAdd)
omnuAdd = New MenuItem("Secon d Sub")
MainMenu1.MenuI tems(0).MenuIte ms(0).MenuItems .Add(omnuAdd)
omnuAdd = New MenuItem("Third Sub")
AddHandler omnuAdd.Click, AddressOf Me.mnuClick
MainMenu1.MenuI tems(0).MenuIte ms(0).MenuItems (0).MenuItems.A dd(omnuAdd)
End Sub
Private Sub mnuClick(ByVal sender As System.Object, ByVal e As
System.EventArg s)
Dim oMnu As MenuItem = CType(sender, MenuItem)
Dim oMnuParent As MenuItem = oMnu.Parent
oMnuParent.Menu Items.Add("Adde d to Parent")
oMnuParent = oMnuParent.Pare nt
oMnuParent.Menu Items.Add("Adde d to GrandParent")
End Sub
Private Sub ButtonStrip_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
Dim oStripParent As ToolStripMenuIt em
Dim oStripAdd As New ToolStripMenuIt em("Menu Strip Level")
Me.MenuStrip1.I tems.Add(oStrip Add)
oStripParent = Me.MenuStrip1.I tems(Me.MenuStr ip1.Items.Count - 1)
oStripAdd = New ToolStripMenuIt em("First Sub")
oStripParent.Dr opDownItems.Add (oStripAdd)
oStripParent = oStripParent.Dr opDownItems(oSt ripParent.DropD ownItems.Count
- 1)
oStripAdd = (New ToolStripMenuIt em("Second Sub"))
oStripParent.Dr opDownItems.Add (oStripAdd)
oStripParent = oStripParent.Dr opDownItems(oSt ripParent.DropD ownItems.Count
- 1)
oStripAdd = (New ToolStripMenuIt em("Third Sub"))
AddHandler oStripAdd.Click , AddressOf Me.stripClick
oStripParent.Dr opDownItems.Add (oStripAdd)
End Sub
Private Sub stripClick(ByVa l sender As System.Object, ByVal e As
System.EventArg s)
Dim oStrip As ToolStripMenuIt em = CType(sender, ToolStripItem)
Dim oStripParent As ToolStrip = oStrip.GetCurre ntParent
oStripParent.It ems.Add("Added to Parent")
Dim oStripParent1 As ToolStrip = oStripParent.Pa rent
Dim oStripAdd As New ToolStripMenuIt em("Added to GrandParent")
Try
oStripParent1.I tems.Add(oStrip Add)
Catch ex As Exception
MsgBox("You crash here")
End Try
End Sub
Thank you