Class

# DesktopMenuItem

<div class="rst-class">

forsearch

</div>

Menubar

<div class="rst-class">

forsearch

</div>

Menu

<div class="rst-class">

forsearch

</div>

MenuItem

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

## 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<desktopmenuitem.autoenabled>`   | `Boolean</api/data_types/boolean>` |           |        |
| `Enabled<desktopmenuitem.enabled>`           | `Boolean</api/data_types/boolean>` |           |        |
| `HasCheckMark<desktopmenuitem.hascheckmark>` | `Boolean</api/data_types/boolean>` |           |        |
| `Icon<desktopmenuitem.icon>`                 | `Picture</api/graphics/picture>`   |           |        |
| `Index<desktopmenuitem.index>`               | `Integer</api/data_types/integer>` | ✓         |        |
| `LastRowIndex<desktopmenuitem.lastrowindex>` | `Integer</api/data_types/integer>` | ✓         |        |
| `Name<desktopmenuitem.name>`                 | `String</api/data_types/string>`   |           |        |
| `Shortcut<desktopmenuitem.shortcut>`         | `String</api/data_types/string>`   |           |        |
| `Tag<desktopmenuitem.tag>`                   | `Variant</api/data_types/variant>` |           |        |
| `Text<desktopmenuitem.text>`                 | `String</api/data_types/string>`   |           |        |
| `Visible<desktopmenuitem.visible>`           | `Boolean</api/data_types/boolean>` |           |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                         | Parameters                                                                                                     | Returns                                     | Shared |
|----------------------------------------------|----------------------------------------------------------------------------------------------------------------|---------------------------------------------|--------|
| `AddMenu<desktopmenuitem.addmenu>`           | item As DesktopMenuItem                                                                                        |                                             |        |
| `AddMenuAt<desktopmenuitem.addmenuat>`       | index As `Integer</api/data_types/integer>`, item As DesktopMenuItem                                           |                                             |        |
| `Child<desktopmenuitem.child>`               | name As `String</api/data_types/string>`                                                                       | DesktopMenuItem                             |        |
| `Clone<desktopmenuitem.clone>`               |                                                                                                                | DesktopMenuItem                             |        |
| `Close<desktopmenuitem.close>`               |                                                                                                                |                                             |        |
| `Constructor<desktopmenuitem.constructor0>`  | copy As DesktopMenuItem                                                                                        |                                             |        |
|                                              | text As `String</api/data_types/string>`, tag As `Variant</api/data_types/variant>` = `Nil</api/language/nil>` |                                             |        |
| `Count<desktopmenuitem.count>`               |                                                                                                                | `Integer</api/data_types/integer>`          |        |
| `Handle<desktopmenuitem.handle>`             |                                                                                                                | `Ptr</api/data_types/additional_types/ptr>` |        |
|                                              | type As `DesktopMenuItem.HandleTypes<desktopmenuitem.handletypes>`                                             | `Ptr</api/data_types/additional_types/ptr>` |        |
| `MenuAt<desktopmenuitem.menuat>`             | index As `Integer</api/data_types/integer>`                                                                    | DesktopMenuItem                             |        |
| `PopUp<desktopmenuitem.popup>`               |                                                                                                                | DesktopMenuItem                             |        |
|                                              | globalMouseX As `Integer</api/data_types/integer>`, globalMouseY As `Integer</api/data_types/integer>`         | DesktopMenuItem                             |        |
| `RemoveMenuAt<desktopmenuitem.removemenuat>` | index As `Integer</api/data_types/integer>`                                                                    |                                             |        |

## Events

<div class="rst-class">

table-centered_column_4

</div>

| Name                                                 | Parameters | Returns                            |
|------------------------------------------------------|------------|------------------------------------|
| `MenuBarSelected<desktopmenuitem.menubarselected>`   |            |                                    |
| `MenuItemSelected<desktopmenuitem.menuitemselected>` |            | `Boolean</api/data_types/boolean>` |

## 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. |

## Enumerations

<div id="desktopmenuitem.handletypes">

<div class="rst-class">

forsearch

</div>

</div>

DesktopMenuItem.HandleTypes

### HandleTypes

> Specifies the type of Windows OS handle you wish to receive for use with the Handle property.
>
> | Enum               | Description                                                                                                             |
> |--------------------|-------------------------------------------------------------------------------------------------------------------------|
> | WindowsParentHMENU | The HMENU of the <span class="title-ref">DesktopMenuItem</span> if it is a submenu, otherwise it is the parent's HMENU. |
> | WindowsCommandID   | The unique menu item identifier for the DesktopMenuItem, used by various Win32 APIs such as GetMenuItemInfo.            |

## Property descriptions

<div id="desktopmenuitem.autoenabled">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.AutoEnabled

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

> If set to `True</api/language/true>`, the <span class="title-ref">DesktopMenuItem</span> is enabled by default, as long as the `App</api/language/app>` object or frontmost window has a menu handler for the menuitem.
>
> There is no need to put code in the `MenuBarSelected<desktopmenuitem.menubarselected>` 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">DesktopMenuItem</span>, AutoEnabled is also `True</api/language/true>` by default. See the example on dynamic menus on the <span class="title-ref">DesktopMenuItem</span> page.

<div id="desktopmenuitem.enabled">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.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</api/language/true>` in the MenuBarSelected event handler.
>
> You can set it for MenuItems created for use by `contextual menus<desktopuicontrol.constructcontextualmenu>` or for use with the `Popup<desktopmenuitem.popup>` method.
>
> This example is in the MenuBarSelected event handler and it enables a menu.
>
> ``` xojo
> FilePageSetup.Enabled = True
> ```

<div id="desktopmenuitem.hascheckmark">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.HasCheckMark

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

> Indicates whether or not the menu item is checked.

<div id="desktopmenuitem.icon">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.Icon

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

> A picture that is assigned to the <span class="title-ref">DesktopMenuItem</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):
>
> ``` plain
> gsettings set org.gnome.desktop.interface menus-have-icons true
> ```
>
> The following code is in the `Opening<desktopwindow.opening>` event of the main window. The image appears to the left of the menu item's text.
>
> ``` xojo
> EditFind.Icon = RedApple
> ```

<div id="desktopmenuitem.index">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.Index

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

> The number of the selected MenuItem when it is part of an array.
>
> This property is read-only.

<div id="desktopmenuitem.lastrowindex">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.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="desktopmenuitem.name">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.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 menuitem.
>
> ``` xojo
> EditMenu.MenuAt(2).Name = "Delete"
> ```

<div id="desktopmenuitem.shortcut">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.Shortcut

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

> The keyboard shortcut for the <span class="title-ref">DesktopMenuItem</span>.
>
> Use this property to set the shortcut in code. You can also specify a shortcut in the Inspector for the MenuItem.
>
> You can use either a printable key or any of the supported non-printable keys (see the table below) as shortcut keys.
>
> 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`.
>
> The following values are supported for modifier keys:
>
> | Name    |
> |---------|
> | ALT     |
> | CMD     |
> | CTRL    |
> | CONTROL |
> | OPT     |
> | SHIFT   |
>
> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> ALT and OPT are treated as the same key.
>
> </div>
>
> The following values are supported for non-printable keys:
>
> | Name        |
> |-------------|
> | Backspace   |
> | Bksp        |
> | Clear       |
> | Del         |
> | Delete      |
> | Down        |
> | Down Arrow  |
> | DownArrow   |
> | Enter       |
> | Esc         |
> | F1          |
> | F2          |
> | F3          |
> | F4          |
> | F5          |
> | F6          |
> | F7          |
> | F8          |
> | F9          |
> | F10         |
> | F11         |
> | F12         |
> | F13         |
> | F14         |
> | F15         |
> | Help        |
> | Left        |
> | Left Arrow  |
> | LeftArrow   |
> | PageDown    |
> | PageUp      |
> | Return      |
> | ReturnLeft  |
> | ReturnRight |
> | Right       |
> | Right Arrow |
> | RightArrow  |
> | Space       |
> | Tab         |
> | TabLeft     |
> | TabRight    |
> | Up          |
> | Up Arrow    |
> | UpArrow     |

<div id="desktopmenuitem.tag">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.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 `DesktopPopupMenu</api/user_interface/desktop/desktoppopupmenu>` control.
>
> This example is in the Opening event of the main window.
>
> ``` xojo
> EditFind.Tag = "Search"
> ```

<div id="desktopmenuitem.text">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.Text

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

> The value (text) of the menu item.
>
> This example is in the Opening event of the main window.
>
> ``` xojo
> EditFind.Text = "Find..."
> ```

<div id="desktopmenuitem.visible">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.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 code is in the Opening event of the main window:
>
> ``` xojo
> EditFind.Visible = False
> ```

## Method descriptions

<div id="desktopmenuitem.addmenu">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.AddMenu

**AddMenu**(item As DesktopMenuItem)

> Adds the passed <span class="title-ref">DesktopMenuItem</span> to the menu. You can add a Separator by passing the class constant DesktopMenuItem.TextSeparator. Used to build a dynamic menu.
>
> The following code creates a contextual menu in the ConstructContextualMenu event of any `DesktopWindow</api/user_interface/desktop/desktopwindow>` or `DesktopUIControl</api/user_interface/desktop/desktopuicontrol>`. This event passes in the parameter base as <span class="title-ref">DesktopMenuItem</span>.
>
> ``` xojo
> 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.
>
> ``` xojo
> Var m, mNew As DesktopMenuItem
> m = Self.MenuBar
> mNew = New DesktopMenuItem
>
> mNew.Text = "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)
> ```

<div id="desktopmenuitem.addmenuat">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.AddMenuAt

**AddMenuAt**(index As `Integer</api/data_types/integer>`, item As DesktopMenuItem)

> Adds *item* as a <span class="title-ref">DesktopMenuItem</span> at the position indicated by *index*. Index is zero-based.
>
> You can add a Separator by passing the class constant DesktopMenuItem.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 DesktopMenuItem
> editPasteSpecial.Text = "Paste Special..."
> EditMenu.AddMenuAt(5, editPasteSpecial)
> ```

<div id="desktopmenuitem.child">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.Child

**Child**(name As `String</api/data_types/string>`) As DesktopMenuItem

> Looks up menu items by *name* and returns a <span class="title-ref">DesktopMenuItem</span>. Returns `Nil</api/language/nil>` if a child is not found.
>
> The following example gets the text of the Edit \> Cut menuitem.
>
> ``` xojo
> Var c As DesktopMenuItem
> c = EditMenu.Child("EditCut")
> MessageBox(c.Text)
> ```

<div id="desktopmenuitem.clone">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.Clone

**Clone** As DesktopMenuItem

> Makes a copy of the <span class="title-ref">DesktopMenuItem</span> and its `children<desktopmenuitem.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">DesktopMenuItem</span> in different places, use the Clone method to create a new copy of the <span class="title-ref">DesktopMenuItem</span> and its children, if any.
>
> ``` xojo
> Var mi As New DesktopMenuItem("My menu item")
> Var myClone As DesktopMenuItem
>
> myClone = mi.Clone ' Creates an independent copy of "mi"
> ```

<div id="desktopmenuitem.close">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.Close

**Close**

> Removes dynamically created menu items.
>
> This example closes a menu item that was previously created.
>
> ``` xojo
> EditSelectAll.Close
> ```

<div id="desktopmenuitem.constructor0">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.Constructor

**Constructor**(copy As DesktopMenuItem)

> <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">DesktopMenuItem</span> that is a duplicate of the provided <span class="title-ref">DesktopMenuItem</span> *copy*.

**Constructor**(text As `String</api/data_types/string>`, tag As `Variant</api/data_types/variant>` = `Nil</api/language/nil>`)

> Creates a new <span class="title-ref">DesktopMenuItem</span> that uses the passed string as its *text* property and optionally adds the passed *tag*. You can add a Separator by passing the class constant DesktopMenuItem.TextSeparator.
>
> 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 DesktopMenuItem
> editPasteSpecial.Text = "Paste Special..."
> EditMenu.AddMenuAt(5, editPasteSpecial)
> ```
>
> Using this constructor, you can rewrite the code above more concisely:
>
> ``` xojo
> Var editPasteSpecial As New DesktopMenuItem("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 Opening even of a window it will add a new menu to menubar when the window opens.
>
> ``` xojo
> Var m As DesktopMenuItem
> Var mNew As DesktopMenuItem
>
> m = Self.MenuBar
> mNew = New DesktopMenuItem
>
> mNew.Text = "MyMenuText"
> mNew.Name = "MyMenuName"
>
> Var submenu As New DesktopMenuItem("MySub")
>
> submenu.AddMenu(New DesktopMenuItem("Submenu One"))
> submenu.AddMenu(New DesktopMenuItem("Submenu Two"))
> submenu.AddMenu(New DesktopMenuItem("Submenu three"))
>
> mNew.AddMenu(submenu)
>
> m.AddMenu(mNew)
> ```

<div id="desktopmenuitem.count">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.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="desktopmenuitem.handle">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.Handle

**Handle** As `Ptr</api/data_types/additional_types/ptr>`

> Provides access to the appropriate underlying Linux or macOS menu implementation.

**Handle**(type As `DesktopMenuItem.HandleTypes<desktopmenuitem.handletypes>`) As `Ptr</api/data_types/additional_types/ptr>`

> Provides access to the appropriate underlying Windows OS menu implementation.
>
> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> Passing a type as a parameter to this method on Linux or macOS will result in a `PlatformNotSupportedException</api/exceptions/platformnotsupportedexception>` .
>
> </div>

<div id="desktopmenuitem.menuat">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.MenuAt

**MenuAt**(index As `Integer</api/data_types/integer>`) As DesktopMenuItem

> Item returns as a <span class="title-ref">DesktopMenuItem</span> the item indicated by the *index* passed.
>
> If the passed index is out of range, an `OutOfBoundsException</api/exceptions/outofboundsexception>` is raised.
>
> The following example gets the <span class="title-ref">DesktopMenuItem</span> corresponding to the Cut item on the Edit menu by position:
>
> ``` xojo
> Var c As DesktopMenuItem
> c = EditMenu.MenuAt(2)
> MessageBox(c.Text)
> ```

<div id="desktopmenuitem.popup">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.PopUp

**PopUp** As DesktopMenuItem

**PopUp**(globalMouseX As `Integer</api/data_types/integer>`, globalMouseY As `Integer</api/data_types/integer>`) As DesktopMenuItem

> Displays the <span class="title-ref">DesktopMenuItem</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">DesktopMenuItem</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 `DesktopUIControl</api/user_interface/desktop/desktopuicontrol>`. You can get the text of the selected item by accessing the Text property of the returned <span class="title-ref">DesktopMenuItem</span>.
>
> ``` xojo
> Var popMenu As DesktopMenuItem
> popMenu = EditMenu.Clone
>
> Var selectedMenu As DesktopMenuItem
> selectedMenu = popMenu.Popup
> ```

<div id="desktopmenuitem.removemenuat">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.RemoveMenuAt

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

> Removes the <span class="title-ref">DesktopMenuItem</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="desktopmenuitem.menubarselected">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.MenuBarSelected

**MenuBarSelected**

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

<div id="desktopmenuitem.menuitemselected">

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

</div>

<div class="rst-class">

forsearch

</div>

DesktopMenuItem.MenuItemSelected

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

> Called when a <span class="title-ref">DesktopMenuItem</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">DesktopMenuItem</span> page for an example of how a menu that is created dynamically can use the `MenuItemSelected<desktopmenuitem.menuitemselected>` event.

## Notes

<span class="title-ref">DesktopMenuItem</span> objects are used to create and access the properties of menu items. You can change the menu item text using the `Text<desktopmenuitem.text>` property. You can find out if a menu item is checked, or check or uncheck a menu item with the `HasCheckmark<desktopmenuitem.hascheckmark>` property. You can also enable or disable a menu item using the `Enabled<desktopmenuitem.enabled>` property. The `Enabled<desktopmenuitem.enabled>` property should be set only from within an `MenuBarSelected<desktopmenuitem.menubarselected>` event handler. Setting it from anywhere else has no effect.

Three other classes handle specialized menu items. `DesktopQuitMenuItem</api/user_interface/desktop/desktopquitmenuitem>` is designed to manage the File \> Quit (macOS) and File \> Exit (Windows and Linux) menu of a built application; it is enabled by default and automatically calls the `Quit</api/language/quit>` method. The `DesktopPreferencesMenuItem</api/user_interface/desktop/desktoppreferencesmenuitem>` class is designed to handle the Preferences menu item. On 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 `DesktopPreferencesMenuItem</api/user_interface/desktop/desktoppreferencesmenuitem>` 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 `DesktopApplicationMenuItem</api/user_interface/desktop/desktopapplicationmenuitem>` class is designed for creating menu items that appear under the application's menu on macOS. Any menu subclassed from `DesktopApplicationMenuItem</api/user_interface/desktop/desktopapplicationmenuitem>` 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<desktopmenuitem.clone>` method.

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

### Contextual menus

Use The ConstructContextualMenu and ContextualMenuAction events of the `DesktopUIControl</api/user_interface/desktop/desktopuicontrol>` or `DesktopWindow</api/user_interface/desktop/desktopwindow>` to create and manage contextual menus.

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

### MenuItems on macOS

The Mac framework does not allow a <span class="title-ref">DesktopMenuItem</span> to be used in different places, e.g. several `` DesktopMenuBar</api/user_interface/desktop/desktopmenubar>`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">DesktopMenuItem</span>, you can use the `Clone<desktopmenuitem.clone>` method to make copies.

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

### Specifying the keyboard shortcut in the menu editor

A menu item's shortcut key by assigning a key via the `Shortcut<desktopmenuitem.shortcut>` property and (normally) at least one modifier key. The `Shortcut<desktopmenuitem.shortcut>` property can be assigned in the Inspector or via code.

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"
```

In the Inspector you can use the Menu Modifier or Alternate Menu Modifier to assign the appropriate modifiers keys in a platform-independent way. For example, Modifier Key is the `Cmd` key on macOS and `Ctrl` key on Linux and Windows. The Alternate Modifier Key is `Shift` on all platforms. You can additionally choose to enable the Mac Option Key, Mac Control Key and the PC Alt Key (for Linux and Windows) in the Inspector.

These keys can also be assigned in code using "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 select the desired modifier key in the Inspector or via the `Shortcut<desktopmenuitem.shortcut>` property.

## Sample code

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

``` xojo
EditPaste.Text = "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 DesktopMenuItem
EditPasteSpecial.Text = "Paste Special..."
EditMenu.AddMenuAt(5, EditPasteSpecial)
```

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

Using the constructor, you can write:

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

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

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

``` xojo
Var EditSelectAll As New DesktopMenuItem
EditSelectAll.Text = "Select All..."
EditMenu.AddMenu(EditSelectAll)
```

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

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

``` xojo
Var c As DesktopMenuItem
c = EditMenu.Child("EditCut")
MessageBox(c.Text)
```

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

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

``` xojo
Var c As DesktopMenuItem
c = EditMenu.MenuAt(2)
MessageBox(c.Text)
```

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

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 `DesktopUIControl</api/user_interface/desktop/desktopuicontrol>`. You can get the text of the selected item by accessing the Text property of the returned <span class="title-ref">DesktopMenuItem</span>.

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

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

In general you should create and display contextual menus using the ConstructContextualMenu and ContextualMenuSelected event handlers of the `DesktopWindow</api/user_interface/desktop/desktopwindow>` and `DesktopUIControl</api/user_interface/desktop/desktopuicontrol>` 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 `DesktopWindow</api/user_interface/desktop/desktopwindow>` or `DesktopUIControl</api/user_interface/desktop/desktopuicontrol>`. This event passes in the parameter base as <span class="title-ref">DesktopMenuItem</span>.

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

``` xojo
Select Case hitItem.Text
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">DesktopMenuItem</span> in the Project Window. To create the menuitems, you use the <span class="title-ref">DesktopMenuItem</span>'s constructor to instantiate an instance of the class for each menuitem you need. You handle the menuitem in the `MenuItemSelected<desktopmenuitem.menuitemselected>` 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.Opening<desktopapplication.opening>` event or the `Opening<desktopwindow.opening>` event of a window if the menu should appear only for a particular window. This example uses the App.Opening event.

Before adding the App.Opening event, we will add a <span class="title-ref">DesktopMenuItem</span> subclass to the project. We will name it "AddFont" and set its `Super</api/language/super>` class to <span class="title-ref">DesktopMenuItem</span>. It will use the constructor to add each menuitem. The constructor takes the <span class="title-ref">DesktopMenuItem</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">DesktopMenuItem</span> subclass has two events, `MenuItemSelected<desktopmenuitem.menuitemselected>` and `MenuBarSelected<desktopmenuitem.menubarselected>`. You do not need to use `MenuBarSelected<desktopmenuitem.menubarselected>` because `AutoEnabled<desktopmenuitem.autoenabled>` is `True</api/language/true>` by default. You do need to add code to the `MenuItemSelected<desktopmenuitem.menuitemselected>` event. This is the code that will run when a user selects a menuitem. 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 `DesktopTextArea</api/user_interface/desktop/desktoptextarea>` to the font that the user chose. The `MenuItemSelected<desktopmenuitem.menuitemselected>` event is:

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

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

TextWindow is the name of the window that contains the `DesktopTextArea</api/user_interface/desktop/desktoptextarea>` and TextArea1 is the `DesktopTextArea</api/user_interface/desktop/desktoptextarea>`. 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 `DesktopApplication.Opening<desktopapplication.opening>` 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 DesktopMenuItem
m = Self.MenuBar
mNew = New DesktopMenuItem
mNew.Text = "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 DesktopMenuItem
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

|                       |         |
|-----------------------|---------|
| **Project Types**     | Desktop |
| **Operating Systems** | All     |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `DesktopApplicationMenuItem</api/user_interface/desktop/desktopapplicationmenuitem>`, `MenuHasParentException</api/exceptions/menuhasparentexception>`, `DesktopPreferencesMenuItem</api/user_interface/desktop/desktoppreferencesmenuitem>`, `DesktopQuitMenuItem</api/user_interface/desktop/desktopquitmenuitem>` classes; `IsContextualClick</api/user_interface/desktop/iscontextualclick>`, `DesktopApplication.RefreshMenuBar<desktopapplication.refreshmenubar>` functions, `New</api/language/new>` operator.

</div>
