DesktopMenuItem.AddMenu

From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!
Method

DesktopMenuItem.AddMenu(Item as DesktopMenuItem)

New in 2021r3

Supported for all project types and targets.

Adds the passed DesktopMenuItem 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 DesktopWindow or DesktopUIControl. This event passes in the parameter base as DesktopMenuItem.

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

base.AddMenu(New DesktopMenuItem("Cut"))
base.AddMenu(New DesktopMenuItem("Copy"))
base.AddMenu(New DesktopMenuItem("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 Opening event of the App or the window. It’s easiest to create menus and menuitems using the built-in Menu Editor and enable the DesktopMenuItems using the AutoEnable property. Use code for cases such as dynamic menus and contextual menus.

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

mNew.Value = "View"
mNew.Name = "View"

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

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

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

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