MenuItem.AddMenu

From Xojo Documentation

Method

MenuItem.AddMenu(Item as MenuItem)

New in 2019r2

Supported for all project types and targets.

Adds the passed MenuItem to the menu. You can add a Separator by passing the class constant MenuItem.TextSeparator. Used to build a dynamic menu.

Sample Code

The following code creates a contextual menu in the ConstructContextualMenu event of any Window or RectControl. This event passes in the parameter base as MenuItem.

base.AddMenu(New MenuItem("Import"))
base.AddMenu(New MenuItem("Export"))
base.AddMenu(New MenuItem(MenuItem.TextSeparator))

base.AddMenu(New MenuItem("Cut"))
base.AddMenu(New MenuItem("Copy"))
base.AddMenu(New MenuItem("Paste"))
Return True // display the contextual menu

The following code creates a hierarchical menu that is added to the main menubar. It is in the Open event of the App or the window. It’s easiest to create menus and menuitems using the built-in Menu Editor and enable the MenuItems using the AutoEnable property. Use code for cases such as dynamic menus and contextual menus.

Var m, mNew As MenuItem
m = Self.MenuBar
mNew = New MenuItem

mNew.Text = "Fonts"
mNew.Name = "Fonts"

mNew.AddMenu(New MenuItem("TestOne"))
mNew.AddMenu(New MenuItem("Testtwo"))
mNew.AddMenu(New MenuItem("Testthree"))

mNew.AddMenu(New MenuItem( "-" ))

Var submenu As New MenuItem("MySub")
submenu.AddMenu(New MenuItem("submenu One"))
submenu.AddMenu(New MenuItem("submenu Two"))
submenu.AddMenu(New MenuItem("submenu three"))

m.AddMenu(mNew)
mNew.AddMenu(submenu)