<div class="meta" robots="noindex">

</div>

Class

# MenuItem

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<div class="warning">

<div class="title">

Warning

</div>

This item was deprecated in version 2021r3. Please use `DesktopMenuItem</api/user_interface/desktop/desktopmenuitem>` as a replacement.

</div>

## Description

An individual menu item in a desktop project. Used by menu bars and contextual menus.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                                                        | Type                               | Read-Only | Shared |
|-------------------------------------------------------------|------------------------------------|-----------|--------|
| `AutoEnabled<menuitem.autoenabled>`                         | `Boolean</api/data_types/boolean>` |           |        |
| `Enabled<menuitem.enabled>`                                 | `Boolean</api/data_types/boolean>` |           |        |
| `Handle<menuitem.handle>`                                   | `Integer</api/data_types/integer>` | ✓         |        |
| `HasAltKey<menuitem.hasaltkey>`                             | `Boolean</api/data_types/boolean>` | ✓         |        |
| `HasCheckmark<menuitem.hascheckmark>`                       | `Boolean</api/data_types/boolean>` |           |        |
| `HasMacControlKey<menuitem.hasmaccontrolkey>`               | `Boolean</api/data_types/boolean>` | ✓         |        |
| `HasMacOptionKey<menuitem.hasmacoptionkey>`                 | `Boolean</api/data_types/boolean>` | ✓         |        |
| `HasSecondaryModifierKey<menuitem.hassecondarymodifierkey>` | `Boolean</api/data_types/boolean>` | ✓         |        |
| `HasStandardModifierKey<menuitem.hasstandardmodifierkey>`   | `Boolean</api/data_types/boolean>` | ✓         |        |
| `Icon<menuitem.icon>`                                       | `Picture</api/graphics/picture>`   |           |        |
| `Index<menuitem.index>`                                     | `Integer</api/data_types/integer>` | ✓         |        |
| `LastRowIndex<menuitem.lastrowindex>`                       | `Integer</api/data_types/integer>` | ✓         |        |
| `Name<menuitem.name>`                                       | `String</api/data_types/string>`   |           |        |
| `Shortcut<menuitem.shortcut>`                               | `String</api/data_types/string>`   |           |        |
| `ShortcutKey<menuitem.shortcutkey>`                         | `String</api/data_types/string>`   | ✓         |        |
| `Tag<menuitem.tag>`                                         | `Variant</api/data_types/variant>` |           |        |
| `Value<menuitem.value>`                                     | `String</api/data_types/string>`   |           |        |
| `Visible<menuitem.visible>`                                 | `Boolean</api/data_types/boolean>` |           |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                  | Parameters                                                                                                         | Returns                            | Shared |
|---------------------------------------|--------------------------------------------------------------------------------------------------------------------|------------------------------------|--------|
| `AddMenu<menuitem.addmenu>`           | Item As MenuItem                                                                                                   |                                    |        |
| `AddMenuAt<menuitem.addmenuat>`       | Index As `Integer</api/data_types/integer>`, Item As MenuItem                                                      |                                    |        |
| `Child<menuitem.child>`               | Name As `String</api/data_types/string>`                                                                           | MenuItem                           |        |
| `Clone<menuitem.clone>`               |                                                                                                                    | MenuItem                           |        |
| `Close<menuitem.close>`               |                                                                                                                    |                                    |        |
| `Constructor<menuitem.constructor0>`  | Text As `String</api/data_types/string>`, \[Tag As `Variant</api/data_types/variant>` = `Nil</api/language/nil>`\] |                                    |        |
| `Count<menuitem.count>`               |                                                                                                                    | `Integer</api/data_types/integer>` |        |
| `MenuAt<menuitem.menuat>`             | Index As `Integer</api/data_types/integer>`                                                                        | MenuItem                           |        |
| `Popup<menuitem.popup>`               | \[x As `Integer</api/data_types/integer>`, y As `Integer</api/data_types/integer>`\]                               | MenuItem                           |        |
| `Remove<menuitem.remove>`             | Child As MenuItem                                                                                                  |                                    |        |
| `RemoveMenuAt<menuitem.removemenuat>` | Index As `Integer</api/data_types/integer>`                                                                        |                                    |        |

## Events

<div class="rst-class">

table-centered_column_4

</div>

| Name                              | Parameters | Returns                            |
|-----------------------------------|------------|------------------------------------|
| `Action<menuitem.action>`         |            | `Boolean</api/data_types/boolean>` |
| `EnableMenu<menuitem.enablemenu>` |            |                                    |

## Constants

The following class constant is used to add a separator item to a menu. Use this constant to add a separator with the AddMenu or AddMenuAt methods:

| Class Constant | Description       |
|----------------|-------------------|
| TextSeparator  | A menu separator. |

## Property descriptions

<div id="menuitem.autoenabled">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.AutoEnabled

**AutoEnabled** As `Boolean</api/data_types/boolean>`

If set to `True</api/language/true>`, the <span class="title-ref">MenuItem</span> is enabled by default, as long as the `App</api/language/app>` object or frontmost window has a menu handler for the <span class="title-ref">MenuItem</span>.

There is no need to put code in the `EnableMenu<menuitem.enablemenu>` event handler to explicitly enable an autoenabled menu item. AutoEnabled is `True</api/language/true>` by default. When you create dynamic menus by creating a new class based on <span class="title-ref">MenuItem</span>, AutoEnabled is also `True</api/language/true>` by default. See the example on dynamic menus on the <span class="title-ref">MenuItem</span> page.

<div id="menuitem.enabled">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Enabled

**Enabled** As `Boolean</api/data_types/boolean>`

Indicates whether or not the menu item is enabled.

For MenuItems that belong to an App or Window menu, this property should only be set to True in the EnableMenuItems event handler.

You can set it for MenuItems created for use by `contextual menus<rectcontrol.constructcontextualmenu>` or for use with the `Popup<menuitem.popup>` method.

This example is in the EnableMenuItems event handler and it enables a menu.

``` xojo
FilePageSetup.Enabled = True
```

<div id="menuitem.handle">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Handle

**Handle** As `Integer</api/data_types/integer>`

Provides access to the appropriate underlying platform menu implementation.

This property is read-only.

If the requested handle type doesn't make sense for the platform the application is running on, a PlatformNotSupportedException is raised.

**MenuItem.HandleType** enumeration:

- CocoaNSMenuItem: Returns the underlying NSMenuItem. If there is a submenu, the NSMenu can be got through NSMenuItem's submenu method.
- WindowsParentHMENU: Returns the HMENU of the <span class="title-ref">MenuItem</span> if it is a submenu, otherwise it returns the parent's HMENU
- WindowsCommandID: Returns the unique menu item identifier for the <span class="title-ref">MenuItem</span>, used by various Win32 APIs such as GetMenuItemInfo

<div id="menuitem.hasaltkey">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.HasAltKey

**HasAltKey** As `Boolean</api/data_types/boolean>`

The Alt key on Windows and Linux keyboards. If selected, the Alt key is must be held down while pressing the value of the Key property in order to trigger the event handler for the <span class="title-ref">MenuItem</span>. This property is for Windows and Linux only.

This property is read-only.

Use `Shortcut<menuitem.shortcut>` to change this value at runtime.

<div id="menuitem.hascheckmark">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.HasCheckmark

**HasCheckmark** As `Boolean</api/data_types/boolean>`

Indicates whether or not the menu item is checked.

<div id="menuitem.hasmaccontrolkey">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.HasMacControlKey

**HasMacControlKey** As `Boolean</api/data_types/boolean>`

The Control key on Mac. This property is Mac-only. If selected, the Control key must be held down while pressing the value of the `ShortcutKey<menuitem.shortcutkey>` property in order to trigger the event handler of the <span class="title-ref">MenuItem</span>.

This property is read-only.

Use `Shortcut<menuitem.shortcut>` to change this value at runtime.

<div id="menuitem.hasmacoptionkey">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.HasMacOptionKey

**HasMacOptionKey** As `Boolean</api/data_types/boolean>`

The Option key on Mac. This property is Mac-only. If selected, the Option key must be held down while pressing the value of the `ShortcutKey<menuitem.shortcutkey>` property to trigger the event handler of the <span class="title-ref">MenuItem</span>.

This property is read-only.

Use `Shortcut<menuitem.shortcut>` to change this value at runtime.

<div id="menuitem.hassecondarymodifierkey">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.HasSecondaryModifierKey

**HasSecondaryModifierKey** As `Boolean</api/data_types/boolean>`

The Shift key on all platforms. If `True</api/language/true>`, the Shift key must be held down while pressing the value of the Key property to trigger the event handler of the <span class="title-ref">MenuItem</span>.

This property is read-only.

Use `Shortcut<menuitem.shortcut>` to change this value at runtime.

<div id="menuitem.hasstandardmodifierkey">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.HasStandardModifierKey

**HasStandardModifierKey** As `Boolean</api/data_types/boolean>`

The Control key on Windows and Linux and the Command key on Mac. If this property is selected, the MenuModifier key must be held down while pressing the key specified by the `ShortcutKey<menuitem.shortcutkey>` property to trigger the event handler for the <span class="title-ref">MenuItem</span>.

This property is read-only.

Use `Shortcut<menuitem.shortcut>` to change this value at runtime.

<div id="menuitem.icon">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Icon

**Icon** As `Picture</api/graphics/picture>`

A picture that is assigned to the <span class="title-ref">MenuItem</span>.

On macOS and Linux, the picture appears in its original size. Resize the icon externally before adding it to the project. On Windows, the icon is resized so that its size matches the height of the item's Text. In most cases icons that are 16x16 points look best.

Recent versions of Linux (that use recent version of the GNOME desktop manager), default to not showing menu icons. You can change the OS setting to display menu icons using terminal [](http://gnomeshell.wordpress.com/2011/09/11/show-and-hide-the-menu-icons/):

> gsettings set org.gnome.desktop.interface menus-have-icons true

The following code is in the `Open<window.open>` event of the main window. The image appears to the left of the menu item's text.

``` xojo
EditFind.Icon = RedApple
```

<div id="menuitem.index">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Index

**Index** As `Integer</api/data_types/integer>`

The number of the selected <span class="title-ref">MenuItem</span> when it is part of an array.

This property is read-only.

<div id="menuitem.lastrowindex">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.LastRowIndex

**LastRowIndex** As `Integer</api/data_types/integer>`

The index of the last child menu item owned by this menu.

This property is read-only.

<div id="menuitem.name">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Name

**Name** As `String</api/data_types/string>`

The name of the menu item. Set this property using the Menu Editor.

This example sets the name of a <span class="title-ref">MenuItem</span>.

``` xojo
EditMenu.Item(2).Name = "Delete"
```

<div id="menuitem.shortcut">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Shortcut

**Shortcut** As `String</api/data_types/string>`

The keyboard shortcut for the <span class="title-ref">MenuItem</span>.

Use this property to set the shortcut in code. You can also specify a shortcut in the Inspector for the <span class="title-ref">MenuItem</span>.

You can use either a printable key or the following non-printable keys as shortcut keys: F1-F15, Tab, Enter, Space, Del (Delete), Return, Bksp (Backspace), Esc, Clear, PageUp, PageDown, Left, Right, Up, Down, Help, and Ins (Insert).

If the shortcut key is more than one character and it is being set via code, the modifier key is not implied and must be explicitly defined. For example, the following line sets the Tab key as the shortcut key and uses the Ctrl key as the modifier.

``` xojo
SpecialMyMenuItem.Shortcut = "Ctrl-Tab"
```

For macOS, use "Cmd" instead of "Ctrl." Or set the `HasStandardModifierKey<menuitem.hasstandardmodifierkey>` property to `True</api/language/true>` in the IDE and pass the string literal for the desired keyboard shortcut.

<div id="menuitem.shortcutkey">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.ShortcutKey

**ShortcutKey** As `String</api/data_types/string>`

The shortcut key for the menu item. If this key and the selected modifier keys are held down, the event handler for the menu item will be executed as if the <span class="title-ref">MenuItem</span> itself were chosen via the mouse pointer.

This property is read-only.

When you use the Inspector for the Menu Editor, you specify the menu item's shortcut key by assigning a key to the Key property and (normally) at least one modifier key. The Menu Editor translates your settings into the value for the `Shortcut<menuitem.shortcut>` property.

You can use either a printable key or the following non-printable keys as shortcut keys: F1-F15, Tab, Enter, Space, Del (Delete), Return, Bksp (Backspace), Esc, Clear, PageUp, PageDown, Left, Right, Up, Down, Help, and Ins (Insert).

To set the shortcut in code, use the `Shortcut<menuitem.shortcut>` property. If the shortcut key is more than one character and it is being set via code, the modifier key is not implied and must be explicitly defined. For example, the following line sets the Tab key as the shortcut key and uses the Ctrl key as the modifier.

``` xojo
SpecialMyMenuItem.Shortcut = "Ctrl-Tab"
```

For macOS, use "Cmd" instead of "Ctrl." Or set the `HasStandardModifierKey<menuitem.hasstandardmodifierkey>` property to `True</api/language/true>` in the IDE and pass the string literal for the desired keyboard shortcut.

<div id="menuitem.tag">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Tag

**Tag** As `Variant</api/data_types/variant>`

A “hidden” value associated with the menu item.

The tag is accessible via code when the user chooses the menu item but, unlike the Text property, is not displayed in the menu. It works like the RowTag property of a `PopupMenu</api/deprecated/popupmenu>` control.

This example is in the Open event of the main window.

``` xojo
EditFind.Tag = "Search"
```

<div id="menuitem.value">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Value

**Value** As `String</api/data_types/string>`

The value (text) of the menu item.

This example is in the Open event of the main window.

``` xojo
EditFind.Value = "Find..."
```

<div id="menuitem.visible">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Visible

**Visible** As `Boolean</api/data_types/boolean>`

Indicates whether or not the menu item is visible. The default is `True</api/language/true>`. If `False</api/language/false>`, the menu item does not appear and no blank space is for it is in the menu.

This property does nothing on Windows so use `RemoveMenuAt<menuitem.removemenuat>` instead.

This code is in the Open event of the main window:

``` xojo
EditFind.Visible = False
```

## Method descriptions

<div id="menuitem.addmenu">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.AddMenu

**AddMenu**(Item As MenuItem)

Adds the passed <span class="title-ref">MenuItem</span> to the menu. You can add a Separator by passing the class constant <span class="title-ref">MenuItem</span>.TextSeparator. Used to build a dynamic menu.

The following code creates a contextual menu in the ConstructContextualMenu event of any `Window</api/deprecated/window>` or `RectControl</api/deprecated/rectcontrol>`. This event passes in the parameter base as <span class="title-ref">MenuItem</span>.

``` xojo
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.

``` xojo
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)
```

<div id="menuitem.addmenuat">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.AddMenuAt

**AddMenuAt**(Index As `Integer</api/data_types/integer>`, Item As MenuItem)

Adds *Item* as a <span class="title-ref">MenuItem</span> at the position indicated by *Index*. Index is zero-based.

You can add a Separator by passing the class constant <span class="title-ref">MenuItem</span>.TextSeparator.

The following example adds a new item in the Edit menu with the text "Paste Special..." just below the Paste item.

``` xojo
Var editPasteSpecial As New MenuItem
editPasteSpecial.Text = "Paste Special..."
EditMenu.AddMenuAt(5, editPasteSpecial)
```

<div id="menuitem.child">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Child

**Child**(Name As `String</api/data_types/string>`) As MenuItem

Looks up menu items by Name and returns a <span class="title-ref">MenuItem</span>. Returns `Nil</api/language/nil>` if a child is not found.

The following example gets the text of the Edit \> Cut <span class="title-ref">MenuItem</span>.

``` xojo
Var c As MenuItem
c = EditMenu.Child("EditCut")
MessageBox(c.Value)
```

<div id="menuitem.clone">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Clone

**Clone** As MenuItem

Makes a copy of the <span class="title-ref">MenuItem</span> and its `children<menuitem.child>` if any. Mac does not permit duplicate MenuItems, so you will need to create clones of any MenuItems that are now being used in two or more locations.

Whenever you need to use the same <span class="title-ref">MenuItem</span> in different places, use the Clone method to create a new copy of the <span class="title-ref">MenuItem</span> and its children, if any.

``` xojo
Var mi As New MenuItem("My menu item")
Var myClone As MenuItem

myClone = mi.Clone ' Creates an independent copy of "mi"
```

<div id="menuitem.close">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Close

**Close**

Removes dynamically created menu items.

This example closes a menu item that was previously created.

``` xojo
EditSelectAll.Close
```

<div id="menuitem.constructor0">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Constructor

**Constructor**(Text as `String</api/data_types/string>`, \[Tag as `Variant</api/data_types/variant>` = `Nil</api/language/nil>`\])

<div class="note">

<div class="title">

Note

</div>

`Constructors</api/language/constructor>` are special methods called when you create an object with the `New</api/language/new>` keyword and pass in the parameters above.

</div>

Creates a new <span class="title-ref">MenuItem</span> that uses the passed string as its Text property and optionally adds the passed Tag.

This example inserts a new item in the Edit menu with the text "Paste Special..." just below the Paste item.

``` xojo
Var editPasteSpecial As New MenuItem
editPasteSpecial.Text = "Paste Special..."
EditMenu.AddMenuAt(5, editPasteSpecial)
```

Using this constructor, you can rewrite the code above more concisely:

``` xojo
Var editPasteSpecial As New MenuItem("Paste Special...")
EditMenu.AddMenuAt(5, editPasteSpecial)
```

This example illustrates how you can manipulate the menu bar at run time. By adding this example to the open even of a window it will add a new menu to menubar when the window opens.

``` xojo
Var m As MenuItem
Var mNew As MenuItem

m = Self.MenuBar
mNew = New MenuItem

mNew.Text = "MyMenuText"
mNew.Name = "MyMenuName"

Var submenu As New MenuItem("MySub")

submenu.AddMenu(New MenuItem("Submenu One"))
submenu.AddMenu(New MenuItem("Submenu Two"))
submenu.AddMenu(New MenuItem("Submenu three"))

mNew.AddMenu(submenu)

m.AddMenu(mNew)
```

<div id="menuitem.count">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Count

**Count** As `Integer</api/data_types/integer>`

Returns as an `Integer</api/data_types/integer>` the number of children a menu owns.

For a menu item, it returns the number of submenu items, if any. If there are no submenu items, it returns zero.

<div id="menuitem.menuat">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.MenuAt

**MenuAt**(Index As `Integer</api/data_types/integer>`) As MenuItem

Item returns as a <span class="title-ref">MenuItem</span> the item indicated by its index (zero-based).

If the passed index is out of range, an `OutOfBoundsException</api/exceptions/outofboundsexception>` is raised.

The following example gets the <span class="title-ref">MenuItem</span> corresponding to the Cut item on the Edit menu by position:

``` xojo
Var c As MenuItem
c = EditMenu.MenuAt(2)
MessageBox(c.Value)
```

<div id="menuitem.popup">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Popup

**Popup**(\[x As `Integer</api/data_types/integer>`, y As `Integer</api/data_types/integer>`\]) As MenuItem

Displays the <span class="title-ref">MenuItem</span> as a contextual menu.

If no parameters are passed, the contextual menu appears at the location of the mouse pointer. If you pass the optional parameters, the contextual menu appears at the passed location. The coordinates are global, not just in the object that handles the MouseDown event.

Popup returns the selected item as a <span class="title-ref">MenuItem</span>. The selected item's Action event will be fired. If the selected item is handled by a MenuHandler that returns `True</api/language/true>`, then PopUp will return `Nil</api/language/nil>`.

The following example displays the Edit menu as a contextual menu. The code is in the MouseDown event handler of a `RectControl</api/deprecated/rectcontrol>`. You can get the text of the selected item by accessing the Text property of the returned <span class="title-ref">MenuItem</span>.

``` xojo
Var popMenu As MenuItem
popMenu = EditMenu.Clone

Var selectedMenu As MenuItem
selectedMenu = popMenu.Popup
```

<div id="menuitem.remove">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Remove

**Remove**(Child As MenuItem)

Removes the <span class="title-ref">MenuItem</span> specified by the *name*.

This code removes the Select All menu item from the Edit menu.

``` xojo
EditMenu.Remove(3)
```

<div id="menuitem.removemenuat">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.RemoveMenuAt

**RemoveMenuAt**(Index As `Integer</api/data_types/integer>`)

Removes the <span class="title-ref">MenuItem</span> specified by its position (index).

This example removes the Select All menu item from the Edit menu.

``` xojo
EditMenu.RemoveMenuAt(3)
```

## Event descriptions

<div id="menuitem.action">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.Action

**Action** As `Boolean</api/data_types/boolean>`

Called when a <span class="title-ref">MenuItem</span> is selected. Return `True</api/language/true>` to prevent the menu event from proceeding any further in the chain of menu handlers.

Refer to the <span class="title-ref">MenuItem</span> page for an example of how a menu that is created dynamically can use the Action event.

<div id="menuitem.enablemenu">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

MenuItem.EnableMenu

**EnableMenu**

Called when a user selects a menu. You can use this to enable/disable/hide menus based on the context of your app.

## Notes

MenuItem objects are used to create and access the properties of menu items. You can change the menu item text using the `Value<menuitem.value>` property. You can find out if a menu item is checked, or check or uncheck a menu item with the `HasCheckmark<menuitem.hascheckmark>` property. You can also enable or disable a menu item using the `Enabled<menuitem.enabled>` property. The `Enabled<menuitem.enabled>` property should be set only from within an `EnableMenu<menuitem.enablemenu>` event handler. Setting it from anywhere else has no effect.

Three other classes handle specialized menu items. `QuitMenuItem</api/deprecated/quitmenuitem>` is designed to manage the Menu \> File \> Quit and Menu \> File \> Exit on Windows and Linux) menu of a built application; it is enabled by default and automatically calls the `Quit</api/language/quit>` method. The `PrefsMenuItem</api/deprecated/prefsmenuitem>` class is designed to handle the Preferences menu item. In macOS, this menu item is supposed to be located under the application's menu, but on other operating systems, this menu does not exist. A menu item derived from the `PrefsMenuItem</api/deprecated/prefsmenuitem>` class automatically appears under the application's menu under macOS; on other operating systems is appears where you put it in the Menu Editor. The `ApplicationMenuItem</api/deprecated/applicationmenuitem>` class is designed for creating menu items that appear under the application's menu on macOS. Any menu subclassed from `ApplicationMenuItem</api/deprecated/applicationmenuitem>` will move to the application's menu for your macOS build but stay where it is for your other builds.

MenuItems can be created on the fly using the `New</api/language/new>` operator or the `Clone<menuitem.clone>` method.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Contextual menus

Use The ConstructContextualMenu and ContextualMenuAction events of the `RectControl</api/deprecated/rectcontrol>` or `Window</api/deprecated/window>` to create and manage contextual menus.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### MenuItems on macos

The Mac framework does not allow a <span class="title-ref">MenuItem</span> to be used in different places, e.g. several `` MenuBar</api/deprecated/menubar>`s. Trying to do so will raise a :doc:`MenuHasParentException</api/exceptions/menuhasparentexception> ``. In order to avoid the hassle of creating several times the same <span class="title-ref">MenuItem</span>, you can use the `Clone<menuitem.clone>` method to make copies.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Specifying the keyboard shortcut in the menu editor

When you use the Properties pane in the Menu Editor, you specify the menu item's shortcut key by assigning a key to the `ShortcutKey<menuitem.shortcutkey>` property and (normally) at least one modifier key. The Menu Editor translates your settings into the value for the `Shortcut<menuitem.shortcut>` property.

You can use either a printable key or the following non-printable keys as shortcut keys: F1-F15, Tab, Enter, Space, Del (Delete), Return, Bksp (Backspace), Esc, Clear, PageUp, PageDown, Left, Right, Up, Down, Help, and Ins (Insert). If the shortcut key is more than one character and it is being set via code, the modifier key is not implied and must be explicitly defined. For example, the following line sets the Tab key as the shortcut key and uses the Ctrl key as the modifier.

``` xojo
SpecialMyMenuItem.Shortcut = "Ctrl-Tab"
```

For macOS, use "Cmd" instead of "Ctrl." Or set the `HasStandardModifierKey<menuitem.hasstandardmodifierkey>` property to `True</api/language/true>` in the IDE and pass the string literal for the desired keyboard shortcut.

Other values you can use are: "Opt", "Alt", "Shift". You can combine keys like this:

``` xojo
MyMenuItem.Shortcut = "Shift-Ctrl-W"
```

When you are setting a non-printable key in the IDE, you can just enter its name from the list above and check the desired modifier key from the following list.

The `ShortcutKey<menuitem.shortcutkey>` property entry and the entries for the modifier keys are described in the table below.

| Name                                                        | Description                                                                                                                                                                                                                                                                                                             |
|-------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `HasSecondaryModifierKey<menuitem.hassecondarymodifierkey>` | The Shift key on all platforms. If selected, the Shift key must be held down while pressing the value of the `ShortcutKey<menuitem.shortcutkey>` property to trigger the event handler of the MenuItem.                                                                                                                 |
| `ShortcutKey<menuitem.shortcutkey>`                         | The shortcut key for the menu item. If this key and the selected modifier keys set is held down, the event handler for the menu item will be executed as if the menu item itself were chosen via the mouse pointer.                                                                                                     |
| `HasMacControlKey<menuitem.hasmaccontrolkey>`               | The Control key on Mac. This property is Mac-only. If selected, the Control key must be held down while pressing the value of the `ShortcutKey<menuitem.shortcutkey>` property in order to trigger the event handler of the menuitem.                                                                                   |
| `HasMacOptionKey<menuitem.hasmacoptionkey>`                 | The Option key on Mac. This property is Mac-only. If selected, the Option key must be held down while pressing the value of the `ShortcutKey<menuitem.shortcutkey>` property to trigger the event handler of the menu item.                                                                                             |
| `HasStandardModifierKey<menuitem.hasstandardmodifierkey>`   | The Control key on Windows and Linux and the Command key on Mac. If this property is selected, the `HasStandardModifierKey<menuitem.hasstandardmodifierkey>` key must be held down while pressing the key specified by the `ShortcutKey<menuitem.shortcutkey>` property to trigger the event handler for the menu item. |
| `HasAltKey<menuitem.hasaltkey>`                             | The Alt key on Windows and Linux keyboards. If selected, the Alt key is must be held down while pressing the value of the `ShortcutKey<menuitem.shortcutkey>` property in order to trigger the event handler for the menu item. This property is for Windows and Linux only.                                            |

## Sample code

The following code changes the text of the EditPaste menu item to "Paste Special…":

``` xojo
EditPaste.Value = "Paste Special..."
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code adds a new item in the Edit menu with the text "Paste Special..." just below the Paste item.

``` xojo
Var EditPasteSpecial As New MenuItem
EditPasteSpecial.Value = "Paste Special..."
EditMenu.AddMenuAt(5, EditPasteSpecial)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Using the constructor, you can write:

``` xojo
Var EditPasteSpecial As New MenuItem("Paste Special...")
EditMenu.AddMenuAt(5, EditPasteSpecial)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code adds a Select All menu item to the Edit menu.

``` xojo
Var EditSelectAll As New MenuItem
EditSelectAll.Value = "Select All..."
EditMenu.AddMenu(EditSelectAll)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code gets the <span class="title-ref">MenuItem</span> corresponding to the Cut item on the Edit menu.

``` xojo
Var c As MenuItem
c = EditMenu.Child("EditCut")
MessageBox(c.Value)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code gets the <span class="title-ref">MenuItem</span> corresponding to the Cut item on the Edit menu by position:

``` xojo
Var c As MenuItem
c = EditMenu.MenuAt(2)
MessageBox(c.Value)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code removes the fourth dynamically created menu item from a menu item array named WindowItem:

``` xojo
WindowItem(3).Close
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code assigns a value to the Tag property of a menu item:

``` xojo
SearchFind.Tag = "UserSearch"
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code displays the Edit menu as a contextual menu. The code is in the MouseDown event handler of a `RectControl</api/deprecated/rectcontrol>`. You can get the text of the selected item by accessing the Text property of the returned <span class="title-ref">MenuItem</span>.

``` xojo
If IsContextualClick Then
  Var m As MenuItem
  m = EditMenu.Popup
End If
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In general you should create and display contextual menus using the ConstructContextualMenu and ContextualMenuAction event handlers of the `Window</api/deprecated/window>` and `RectControl</api/deprecated/rectcontrol>` classes. They are fully cross-platform and don't make assumptions about how the user requested a contextual menu.

The following code creates a contextual menu in the ConstructContextualMenu event of any `Window</api/deprecated/window>` or `RectControl</api/deprecated/rectcontrol>`. This event passes in the parameter base as <span class="title-ref">MenuItem</span>.

``` xojo
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 in the ContextualMenuAction event handles the menu selection. This event passes in the parameter hitItem as <span class="title-ref">MenuItem</span>. This is the selected menu item.

``` xojo
Select Case hitItem.Value
Case "Import"
  MessageBox("You chose Import")
Case "Export"
  MessageBox("You chose export")
Case "Cut"
  MessageBox("You chose Cut")
Case "Copy"
  MessageBox("You chose Copy")
Case "Paste"
  MessageBox("You chose Paste")
End Select

Return True
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In certain cases you cannot specify the menu items that belong in a menu in advance. They may change depending on the context in which the application is used or on the operating system on which the application is running.

A common example of this is the Font menu that is normally included in any application that supports styled text. The programmer has no way of knowing in advance which fonts happen to be installed on the user's computer. The Font menu's menuitems have to be built dynamically when the application is launched.

The recommended technique is to create a subclass of <span class="title-ref">MenuItem</span> in the Project Window. To create the menuitems, you use the <span class="title-ref">MenuItem</span>'s constructor to instantiate an instance of the class for each <span class="title-ref">MenuItem</span> you need. You handle the <span class="title-ref">MenuItem</span> in the `Action<menuitem.action>` event.

You then write code to populate the menu with the names of the fonts installed on the user's computer. If we assume that fonts won't be added or deleted while the application is running, we can build the font list when the application launches. You can do this in the `App.Open<application.open>` event or the `Open<window.open>` event of a window if the menu should appear only for a particular window. This example uses the App.Open event.

Before adding the App.Open event, we will add a <span class="title-ref">MenuItem</span> subclass to the project. We will name it "AddFont" and set its `Super</api/language/super>` class to <span class="title-ref">MenuItem</span>. It will use the constructor to add each <span class="title-ref">MenuItem</span>. The constructor takes the <span class="title-ref">MenuItem</span>'s text as its parameter. The optional *rowtag* parameter is not used. Since the constructor is built-in, you do not need to explicitly add a constructor to your code.

The <span class="title-ref">MenuItem</span> subclass has two events, `Action<menuitem.action>` and `EnableMenu<menuitem.enablemenu>`. You do not need to use `EnableMenu<menuitem.enablemenu>` because `AutoEnabled<menuitem.autoenabled>` is `True</api/language/true>` by default. You do need to add code to the `Action<menuitem.action>` event. This is the code that will run when a user selects a <span class="title-ref">MenuItem</span>. It serves the same purpose as the Menu Handler that is used for static menuitems.

In this case, we want the menu item to set the selected text in a `TextArea</api/deprecated/textarea>` to the font that the user chose. The `Action<menuitem.action>` event is:

``` xojo
TextWindow.TextArea1.SelectionFontName = Me.Value
Return True
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

TextWindow is the name of the window that contains the `TextArea</api/deprecated/textarea>` and TextArea1 is the `TextArea</api/deprecated/textarea>`. The Text variable holds the font name that was passed to AddFont when the font was created.

Next, we want to add code to the `Application.Open<application.open>` event to create the Font menu and populate it with the name of the user's installed fonts. The following code adds the Font menu to the default menubar:

``` xojo
Var m, mNew As MenuItem
m = Self.MenuBar
mNew = New MenuItem
mNew.Value = "Fonts"
mNew.Name = "FontsMenu"
m.AddMenu(mNew)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The next block of code populates the Font menu. It uses the built-in `System.FontAt<system.fontat>` function which returns the name of the ith font on the user's computer. The code instantiates an instance of the AddFont subclass for each font:

``` xojo
Var child As MenuItem
If mNew = Nil Then
  MessageBox("Menu parent is nil!")
  Return
End If

' build the font menu
For i As Integer = 0 To System.LastFontIndex
  child = New AddFont(System.FontAt(i))
  mNew.AddMenu(child)
Next
```

## Compatibility

All project types on all supported operating systems.

## See also

`Object</api/data_types/additional_types/object>` parent class; `ApplicationMenuItem</api/deprecated/applicationmenuitem>`, `MenuHasParentException</api/exceptions/menuhasparentexception>`, `PrefsMenuItem</api/deprecated/prefsmenuitem>`, `QuitMenuItem</api/deprecated/quitmenuitem>` classes; `IsContextualClick</api/user_interface/desktop/iscontextualclick>`, `Application.RefreshMenuBar<application.refreshmenubar>` functions, `New</api/language/new>` operator.
