MenuItem.AddMenu

From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!
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.Value = "View"
mNew.Name = "View"

mNew.AddMenu(New MenuItem("As Icons"))
mNew.AddMenu(New MenuItem("As List"))
mNew.AddMenu(New MenuItem("As Columns"))

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

Var submenu As New MenuItem("Sort By")
submenu.AddMenu(New MenuItem("Name"))
submenu.AddMenu(New MenuItem("Kind"))
submenu.AddMenu(New MenuItem("Size"))

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