Class
DesktopQuitMenuItem
Description
The Quit menu item is derived from the DesktopQuitMenuItem class.
Properties
Name |
Type |
Read-Only |
Shared |
---|---|---|---|
✓ |
|||
✓ |
|||
Methods
Name |
Parameters |
Returns |
Shared |
---|---|---|---|
Item As DesktopMenuItem |
|||
Index As Integer, Item As DesktopMenuItem |
|||
Name As String |
|||
type As HandleTypes |
|||
Index As Integer |
|||
Index As Integer |
Events
Name |
Parameters |
Returns |
---|---|---|
Property descriptions
DesktopQuitMenuItem.AutoEnabled
AutoEnabled As Boolean
If set to True, the DesktopMenuItem is enabled by default, as long as the App object or frontmost window has a menu handler for the menuitem.
There is no need to put code in the MenuBarSelected event handler to explicitly enable an autoenabled menu item. AutoEnabled is True by default. When you create dynamic menus by creating a new class based on DesktopMenuItem, AutoEnabled is also True by default. See the example on dynamic menus on the DesktopMenuItem page.
DesktopQuitMenuItem.Enabled
Enabled As 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 MenuBarSelected event handler.
You can set it for MenuItems created for use by contextual menus or for use with the Popup method.
This example is in the MenuBarSelected event handler and it enables a menu.
FilePageSetup.Enabled = True
DesktopQuitMenuItem.HasCheckMark
HasCheckMark As Boolean
Indicates whether or not the menu item is checked.
DesktopQuitMenuItem.Icon
Icon As Picture
A picture that is assigned to the DesktopMenuItem.
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 Opening event of the main window. The image appears to the left of the menu item's text.
EditFind.Icon = RedApple
DesktopQuitMenuItem.Index
Index As Integer
The number of the selected MenuItem when it is part of an array.
This property is read-only.
DesktopQuitMenuItem.LastRowIndex
LastRowIndex As Integer
The index of the last child menu item owned by this menu.
This property is read-only.
DesktopQuitMenuItem.Name
Name As String
The name of the menu item. Set this property using the Menu Editor.
This example sets the name of a menuitem.
EditMenu.MenuAt(2).Name = "Delete"
DesktopQuitMenuItem.Shortcut
Shortcut As String
The keyboard shortcut for the DesktopMenuItem.
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 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.
SpecialMyMenuItem.Shortcut = "Ctrl-Tab"
For macOS, use Cmd instead of Ctrl. Or set the HasStandardModifierKey property to True in the IDE and pass the string literal for the desired keyboard shortcut.
DesktopQuitMenuItem.Tag
Tag As 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 control.
This example is in the Opening event of the main window.
EditFind.Tag = "Search"
DesktopQuitMenuItem.Text
Text As String
The value (text) of the menu item.
This example is in the Opening event of the main window.
EditFind.Text = "Find..."
DesktopQuitMenuItem.Visible
Visible As Boolean
Indicates whether or not the menu item is visible. The default is True. If 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 DesktopMenuItem instead.
This code is in the Opening event of the main window:
EditFind.Visible = False
Method descriptions
DesktopQuitMenuItem.AddMenu
AddMenu(Item As DesktopMenuItem)
Adds the passed DesktopMenuItem 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 or DesktopUIControl. This event passes in the parameter base as DesktopMenuItem.
base.AddMenu(New DesktopMenuItem("Import"))
base.AddMenu(New DesktopMenuItem("Export"))
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
base.AddMenu(New DesktopMenuItem("Cut"))
base.AddMenu(New DesktopMenuItem("Copy"))
base.AddMenu(New DesktopMenuItem("Paste"))
Return True ' display the contextual menu
The following code creates a hierarchical menu that is added to the main menubar. It is in the Opening event of the App or the window. It's easiest to create menus and menuitems using the built-in Menu Editor and enable the DesktopMenuItems using the AutoEnable property. Use code for cases such as dynamic menus and contextual menus.
Var m, mNew As DesktopMenuItem
m = Self.MenuBar
mNew = New DesktopMenuItem
mNew.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)
DesktopQuitMenuItem.AddMenuAt
AddMenuAt(Index As Integer, Item As DesktopMenuItem)
Adds Item as a DesktopMenuItem 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.
Var editPasteSpecial As New DesktopMenuItem
editPasteSpecial.Text = "Paste Special..."
EditMenu.AddMenuAt(5, editPasteSpecial)
DesktopQuitMenuItem.Child
Child(Name As String) As DesktopMenuItem
Looks up menu items by Name and returns a DesktopMenuItem. Returns Nil if a child is not found.
The following example gets the text of the Edit > Cut menuitem.
Var c As DesktopMenuItem
c = EditMenu.Child("EditCut")
MessageBox(c.Text)
DesktopQuitMenuItem.Clone
Clone As DesktopMenuItem
Makes a copy of the DesktopMenuItem and its children 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 DesktopMenuItem in different places, use the Clone method to create a new copy of the DesktopMenuItem and its children, if any.
Var mi As New DesktopMenuItem("My menu item")
Var myClone As DesktopMenuItem
myClone = mi.Clone ' Creates an independent copy of "mi"
DesktopQuitMenuItem.Close
Close
Removes dynamically created menu items.
This example closes a menu item that was previously created.
EditSelectAll.Close
DesktopQuitMenuItem.Constructor
Constructor(Text As String, [Tag As Variant])
Note
Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.
Creates a new DesktopMenuItem 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.
Var editPasteSpecial As New DesktopMenuItem
editPasteSpecial.Text = "Paste Special..."
EditMenu.AddMenuAt(5, editPasteSpecial)
Using this constructor, you can rewrite the code above more concisely:
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.
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)
DesktopQuitMenuItem.Count
Count As Integer
Returns as an 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.
DesktopQuitMenuItem.Handle
Handle As Ptr
Provides access to the appropriate underlying Linux or macOS menu implementation.
Handle(type As HandleTypes) As Ptr
Provides access to the appropriate underlying Windows OS menu implementation.
Note
Passing a type as a parameter to this method on Linux or macOS will result in a PlatformNotSupportedException .
DesktopQuitMenuItem.MenuAt
MenuAt(Index As Integer) As DesktopMenuItem
Item returns as a DesktopMenuItem the item indicated by the index passed.
If the passed index is out of range, an OutOfBoundsException is raised.
The following example gets the DesktopMenuItem corresponding to the Cut item on the Edit menu by position:
Var c As DesktopMenuItem
c = EditMenu.MenuAt(2)
MessageBox(c.Text)
DesktopQuitMenuItem.PopUp
PopUp([x As Integer, y As Integer]) As DesktopMenuItem
Displays the DesktopMenuItem 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 DesktopMenuItem. The selected item's Action event will be fired. If the selected item is handled by a MenuHandler that returns True, then PopUp will return Nil.
The following example displays the Edit menu as a contextual menu. The code is in the MouseDown event handler of a DesktopUIControl. You can get the text of the selected item by accessing the Text property of the returned DesktopMenuItem.
Var popMenu As DesktopMenuItem
popMenu = EditMenu.Clone
Var selectedMenu As DesktopMenuItem
selectedMenu = popMenu.Popup
DesktopQuitMenuItem.RemoveMenuAt
RemoveMenuAt(Index As Integer)
Removes the DesktopMenuItem specified by its position (index).
This example removes the Select All menu item from the Edit menu.
EditMenu.RemoveMenuAt(3)
Event descriptions
DesktopQuitMenuItem.MenuBarSelected
MenuBarSelected
Called when a user selects a menu. You can use this to enable/disable/hide menus based on the context of your app.
DesktopQuitMenuItem.MenuItemSelected
MenuItemSelected As Boolean
Called when a DesktopMenuItem is selected. Return True to prevent the menu event from proceeding any further in the chain of menu handlers.
Refer to the DesktopMenuItem page for an example of how a menu that is created dynamically can use the MenuItemSelected event.
Notes
The Quit menu item that is added by default to all applications is an instance of the DesktopQuitMenuItem class. It differs from menu items derived from the DesktopMenuItem class in that it is enabled by default and automatically calls the Quit method to quit the application. Thus, there is no need to enable the menu item via code or write its handler.
On macOS, instances of DesktopQuitMenuItem are moved to the Application menu for you automatically. This prevents multiple DesktopQuitMenuItem entries from appearing in menus (only the first usage appears).
Compatibility
Desktop projects on all supported operating systems.
See also
DesktopMenuItem parent class; New operator, DesktopMenuItem, DesktopPreferencesMenuItem, DesktopApplicationMenuItem classes.