From Xojo Documentation

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

DesktopWindow.ConstructContextualMenu(Base as DesktopMenuItem,x as Integer, y as Integer) As Boolean

New in 2021r3

Supported for all project types and targets.

Fires whenever it is appropriate to display a contextual menu for the window.


This is the recommended way to handle contextual menus because this event figures out whether the user has requested the contextual menu, regardless of how he did it. Returns a Boolean. Base is analogous to the menu bar for the contextual menu. Any items you add to Base will be shown as menu items. If you return False, the event is passed up the parent hierarchy. If you return True, the contextual menu is displayed. The parameters x and y are the mouse locations. If the event was fired because of a non-mouse event, then x and y are both set to -1. See the example of a contextual menu in the examples for the DesktopUIControl class.


The following ConstructContextualMenu event handler builds a menu with three menu items plus a submenu with three additional menu items.

// Add some items
base.Add(New DesktopMenuItem("Test 1" )
base.Add(New DesktopMenuItem("Test 2"))
base.Add(New DesktopMenuItem("Test 3"))

// Add a Separator
base.Add(New DesktopMenuItem(MenuItem.TextSeparator))

// Add a sub menu
Var submenu As New DesktopMenuItem("SubMenu")
submenu.Add(New DesktopMenuItem("SubMenu Test 1"))
submenu.Add(New DesktopMenuItem("SubMenu Test 2"))
submenu.Add(New DesktopMenuItem("SubMenu Test 3"))

// Add a Separator
base.Add(New DesktopMenuItem(MenuItem.TextSeparator))

// Add an item that's on a menu bar so that you can see you don't
// have to handle every item returned.

#If TargetMacOS Then
// because of how quitting the app is handled on macOS you cannot add the FileQuit item on macOS

Return True

See Also

The DesktopUIControl.ConstructContextualMenu event handler for creating contextual menus in controls.