Dynamic hierarchical contextual menu

Hello everybody.

I’m trying to build a hierarchical contextual menu from code, but I don’t know in advance the hierarchy depth, so I would like to add submenu items on demand when the user selects a menuitem with children menuitems to avoid the potential long wait that traversing the tree would take.
Is there a way to do that?

Thanks you very much.

I would suggest that if you have more than 3 levels (including the base level), it might be time to rethink your design

Use the EnableMenu event of each MenuItem:

[code]Class Window1 Inherits Window

Event ConstructContextualMenu(base as MenuItem, x as Integer, y as Integer) As Boolean

Dim menuA As New MenuItem(“A”)
AddHandler menuA.EnableMenu, AddressOf Menu_EnableMenu

Dim menuB As New MenuItem(“B”)
AddHandler menuB.EnableMenu, AddressOf Menu_EnableMenu

Dim menuC As New MenuItem(“C”)
AddHandler menuC.EnableMenu, AddressOf Menu_EnableMenu


Sub Menu_EnableMenu(item As MenuItem)

Select Case item.Text

Case “A”
item.Append(New MenuItem(“A 1”))
item.Append(New MenuItem(“A 2”))
item.Append(New MenuItem(“A 3”))

Case “B”
item.Append(New MenuItem(“B 1”))
item.Append(New MenuItem(“B 2”))
item.Append(New MenuItem(“B 3”))

Case “C”
Dim c1 As New MenuItem(“C 1”)
AddHandler c1.EnableMenu, AddressOf Menu_EnableMenu
item.Append(New MenuItem(“C 2”))
item.Append(New MenuItem(“C 3”))

Case “C 1”
For i As Integer = 0 To 10
item.Append(New MenuItem("C 1 " + Str(i)))



End Class[/code]

Thank you so much, Eli!
That did the trick!

Thank you too Dave, I will see if I re think my approach, but so far, Eli’s solution is what I was needing.