Platforms to show: All Mac Windows Linux Cross-Platform

Back to NSMenuMBS class.

NSMenuMBS.addItem(m as NSMenuItemMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Adds a menu item to the end.

If the menuitem is nil, nothing happens.
The menuitem can only be in one menu.

The menu doesn't references to the RB classes behind, so it's up to you to keep those references if you want to get the events and avoid crashes.
The StatusItem example keeps an array with all those menu item objects it needs so RB will not destroy them.

NSMenuMBS.cancelTracking

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Dismisses the menu and ends all menu tracking.

Available in Mac OS X v10.5 and later.

NSMenuMBS.cancelTrackingWithoutAnimation

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Dismisses the menu and ends all menu tracking without displaying the associated animation.

Available in Mac OS X v10.6 and later.

NSMenuMBS.Constructor(Handle as Integer)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 15.2 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Constructor for creating an instanced based on an existing handle.

Useful if you get a NSMenu reference from a declare.
The object is retained.

See also:

NSMenuMBS.Constructor(title as string="")

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 7.4 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Constructor for a new nsmenu.
Example
dim m as NSMenuMBS
m=new NSMenuMBS("Hello World")
msgbox m.title // shows "Hello World"

Title is optional.

See also:

NSMenuMBS.indexOfItem(item as NSMenuItemMBS) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Returns the index identifying the location of a specified menu item in the receiver.

Returns the integer index of the menu item or, if no such menu item is in the menu, –1.

NSMenuMBS.indexOfItemWithSelector(selector as string) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 13.1 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Searches menu items for one with the given selector.

Returns -1 if not found.
You can use this to find menu items by their selector. Which is often more save than by index or title.

NSMenuMBS.indexOfItemWithSubmenu(item as NSMenuMBS) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Returns the index of the menu item in the receiver with the given submenu.

item: A menu object that is a menu item of the receiver (that is, a submenu).

The integer index of the menu item or, if no such menu item is in the menu, –1.

NSMenuMBS.indexOfItemWithTag(tag as Integer) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Returns the index of the first menu item in the receiver identified by a tag.

Returns the integer index of the menu item or, if no such menu item is in the menu, –1.

NSMenuMBS.indexOfItemWithTitle(title as string) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Returns the index of the first menu item in the receiver that has a specified title.

The integer index of the menu item or, if no such menu item is in the menu, –1.

NSMenuMBS.insertItem(m as NSMenuItemMBS, index as Integer)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Inserts a menu item into the menu at a specific location.

If the menuitem is nil, nothing happens. A menuitem can only be part of one menu.

index: An integer index identifying the location of the menu item in the menu. Zero based.

NSMenuMBS.Item(index as Integer) as NSMenuItemMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Returns the item with the given index.

Index is from 0 to numberOfItems-1.
Remember: The objects returned are not the same Xojo objects used with additem or insertitem.

NSMenuMBS.itemWithSelector(selector as string) as NSMenuItemMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 13.1 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Searches menu items for one with the given selector.

You can use this to find menu items by their selector. Which is often more save than by index or title.

NSMenuMBS.performActionForItemAtIndex(index as Integer)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Causes the application to send the action message of a specified menu item to its target.

If a target is not specified, the message is sent to the first responder. As a side effect, this method posts NSMenuWillSendActionNotification and NSMenuDidSendActionNotification.

In Mac OS X v10.6 and laster the performActionForItemAtIndex no longer triggers menu validation. This is because validation is typically done during menu tracking or key equivalent matching, so the subsequent performActionForItemAtIndex validation was redundant. To trigger validation explicitly, use invoke the update method.

In Mac OS X v10.6 performActionForItemAtIndex, when called, now triggers highlighting in the menu bar. It also sends out appropriate accessibility notifications indicating the item was selected.

NSMenuMBS.popUpMenuPositioningItem(item as NSMenuItemMBS, location as NSPointMBS, view as NSViewMBS = nil) as boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 14.2 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Pops up the menu at the specified location.
Example
// create menu
dim m as new NSMenuMBS
m.autoenablesItems = false

// add some items
dim it as new NSMenuItemMBS
it.CreateMenuItem "Hello"
it.Enabled = true
m.addItem it

it = new NSMenuItemMBS
it.CreateMenuItem "World"
it.Enabled = true
m.addItem it

// show relative to this view
dim v as NSViewMBS = window1.PushButton1.NSViewMBS

dim r as Boolean
r = m.popUpMenuPositioningItem(nil, nil, v)
// you need to use NSMenuItemMBS subclasses with action event to get an event for which item was selected...

item: The menu item to be positioned at the specified location in the view.
location: The location in the view coordinate system to display the menu item.
view: The view to display the menu item over.

Returns true if menu tracking ended because an item was selected, and false if menu tracking was cancelled for any reason.

Pops up the receiver as a popup menu. The top left corner of the specified item (if specified, item must be present in the receiver) is positioned at the specified location in the specified view, interpreted in the view's own coordinate system.

If item is nil, the menu is positioned such that the top left of the menu content frame is at the given location.
If view is nil, the location is interpreted in the screen coordinate system. This allows you to pop up a menu disconnected from any window.

Available in OS X v10.6 and later.

Some examples using this method:

NSMenuMBS.removeAllItems

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Removes all the menu items in the receiver.
Example
dim n as NSMenuMBS // your menu

// for Mac OS X 10.6
n.removeAllItems

// for any Mac OS X version
for i as Integer = n.numberOfItems-1 DownTo 0
n.removeItemAtIndex i
next

This method is more efficient than removing menu items individually.
Unlike the other remove methods, this method does not post NSMenuDidChangeItemNotification notifications.
Available in Mac OS X v10.6 and later.

NSMenuMBS.removeItem(m as NSMenuItemMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Removes the menuitem.

Does nothing if menuitem is nil.

The Xojo object used to add the menuitem must not be the same as the one you use here, but the values for the menuitems handle property must match.

NSMenuMBS.removeItemAtIndex(index as Integer)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Removes the menu item with the given index.
Example
dim n as NSMenuMBS // your menu

// for Mac OS X 10.6
n.removeAllItems

// for any Mac OS X version
for i as Integer = n.numberOfItems-1 DownTo 0
n.removeItemAtIndex i
next

Index is from 0 to NumberOfItems-1.

NSMenuMBS.selectedItems as NSMenuItemMBS()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 23.5 ✅ Yes ❌ No ❌ No ❌ No Desktop only
The menu items that are selected.

An item is selected when its state is `NSControl.StateValue.on`.

Requires macOS Sonoma or newer.

NSMenuMBS.setSelectedItems(items() as NSMenuItemMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 23.5 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Sets the menu items that are selected.

An item is selected when its state is `NSControl.StateValue.on`.

Setting `selectedItems` will select any items that are contained in the provided array, and deselect any previously selected items that are not in the array.

Requires macOS Sonoma or newer.

NSMenuMBS.update

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Menus MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Enables or disables the receiver's menu items based on the NSMenuValidation informal protocol and sizes the menu to fit its current menu items if necessary.

The items on this page are in the following plugins: MBS MacBase Plugin.


The biggest plugin in space...