Events
Events
Section titled “Events”Every event in MenuAPI is a member of a Menu, so you always subscribe on the menu that the item belongs to — not on the item itself.
menu.OnItemSelect += (_menu, _item, _index) =>{ Debug.WriteLine($"'{_item.Text}' was pressed.");};MenuAPI uses plain delegates, so a lambda, a new EventHandler(...) style delegate or a normal method all work:
// Lambdamenu.OnMenuOpen += (_menu) => Debug.WriteLine("Opened!");
// Method groupmenu.OnMenuOpen += HandleMenuOpen;
private void HandleMenuOpen(Menu menu){ Debug.WriteLine($"'{menu.MenuTitle}' was opened.");}Which event fires for which item?
Section titled “Which event fires for which item?”| Item type | Pressing select | Pressing left / right |
|---|---|---|
| MenuItem | OnItemSelect | Right selects the item, left goes back to the parent menu. |
| MenuCheckboxItem | OnCheckboxChange | Right toggles the checkbox (so it also raises OnCheckboxChange). |
| MenuListItem | OnListItemSelect | OnListIndexChange |
| MenuDynamicListItem | OnDynamicListItemSelect | OnDynamicListItemCurrentItemChange |
Two more events are not tied to a specific item type: OnIndexChange (the highlighted item changed) and OnMenuOpen / OnMenuClose.
Full example
Section titled “Full example”Menu menu = new Menu("Main Menu", "Subtitle");MenuController.AddMenu(menu);
MenuItem button = new MenuItem("Normal Button", "A simple button.");MenuCheckboxItem box = new MenuCheckboxItem("Checkbox", "A checkbox.", false);MenuListItem list = new MenuListItem("List", new List<string>() { "One", "Two", "Three" }, 0);
menu.AddMenuItem(button);menu.AddMenuItem(box);menu.AddMenuItem(list);
menu.OnItemSelect += (_menu, _item, _index) =>{ if (_item == button) { Debug.WriteLine("The normal button was pressed!"); }};
menu.OnCheckboxChange += (_menu, _item, _index, _checked) =>{ Debug.WriteLine($"'{_item.Text}' is now {(_checked ? "checked" : "unchecked")}.");};
menu.OnListIndexChange += (_menu, _listItem, _oldIndex, _newIndex, _itemIndex) =>{ Debug.WriteLine($"List changed to '{_listItem.GetCurrentSelection()}'.");};
menu.OnListItemSelect += (_menu, _listItem, _listIndex, _itemIndex) =>{ Debug.WriteLine($"'{_listItem.ListItems[_listIndex]}' was picked.");};
menu.OnMenuOpen += (_menu) => Debug.WriteLine("Menu opened.");menu.OnMenuClose += (_menu) => Debug.WriteLine("Menu closed.");OnItemSelect
Section titled “OnItemSelect”Triggered when a MenuItem is pressed. This only fires for plain menu items, not for checkbox or list items.
If the item is bound to a submenu with MenuController.BindMenuItem(), this event fires before the submenu opens.
Disabled items (Enabled = false) never raise this event; they play the error sound instead.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
| menu | Menu | The menu in which this event occurred. |
| menuItem | MenuItem | The item that was pressed. |
| itemIndex | int | The index of that item in the menu. |
menu.OnItemSelect += (_menu, _item, _index) =>{ // Comparing against the item you created is the most reliable check. if (_item == deleteButton) { DeleteTheThing(); }
// Or use ItemData when your items are created in a loop. if (_item.ItemData != null) { SpawnHorse(_item.ItemData.model); }};OnCheckboxChange
Section titled “OnCheckboxChange”Triggered when a MenuCheckboxItem is toggled, either by pressing select on it or by pressing right on it. The Checked property has already been updated when this fires.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
| menu | Menu | The menu in which this event occurred. |
| menuItem | MenuCheckboxItem | The checkbox that was toggled. |
| itemIndex | int | The index of that checkbox in the menu. |
| newCheckedState | boolean | The new checked state. |
menu.OnCheckboxChange += (_menu, _item, _index, _checked) =>{ if (_item == godModeBox) { SetEntityInvincible(PlayerPedId(), _checked); }};OnListItemSelect
Section titled “OnListItemSelect”Triggered when a MenuListItem is pressed (select), not when its value is changed with left/right.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
| menu | Menu | The menu in which this event occurred. |
| listItem | MenuListItem | The list item that was pressed. |
| selectedIndex | int | The currently selected index inside that list item’s ListItems. |
| itemIndex | int | The index of the list item in the menu. |
menu.OnListItemSelect += (_menu, _listItem, _listIndex, _itemIndex) =>{ if (_listItem == weatherList) { SetWeather(_listItem.ListItems[_listIndex]); }};OnListIndexChange
Section titled “OnListIndexChange”Triggered every time the value of a MenuListItem is changed with the left or right control. List items wrap around, so going right on the last value moves back to the first one.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
| menu | Menu | The menu in which this event occurred. |
| listItem | MenuListItem | The list item that was changed. |
| oldSelectionIndex | int | The previously selected index. |
| newSelectionIndex | int | The newly selected index. |
| itemIndex | int | The index of the list item in the menu. |
// Live preview: update something as soon as the value changes.menu.OnListIndexChange += (_menu, _listItem, _oldIndex, _newIndex, _itemIndex) =>{ if (_listItem == timeOfDayList) { SetTimeOfDay(_newIndex); }};OnDynamicListItemCurrentItemChange
Section titled “OnDynamicListItemCurrentItemChange”Triggered every time the value of a MenuDynamicListItem is changed with the left or right control, right after the item’s callback has returned the new value.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
| menu | Menu | The menu in which this event occurred. |
| dynamicListItem | MenuDynamicListItem | The dynamic list item that was changed. |
| oldValue | string | The value before the change. |
| newValue | string | The value the callback returned. |
menu.OnDynamicListItemCurrentItemChange += (_menu, _item, _oldValue, _newValue) =>{ Debug.WriteLine($"'{_item.Text}' changed from {_oldValue} to {_newValue}.");};OnDynamicListItemSelect
Section titled “OnDynamicListItemSelect”Triggered when a MenuDynamicListItem is pressed (select).
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
| menu | Menu | The menu in which this event occurred. |
| dynamicListItem | MenuDynamicListItem | The dynamic list item that was pressed. |
| currentItem | string | The value that is currently being displayed. |
OnIndexChange
Section titled “OnIndexChange”Triggered whenever the highlighted item changes, so every time the user presses up or down. The list wraps around at both ends.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
| menu | Menu | The menu in which this event occurred. |
| oldItem | MenuItem | The item that was highlighted before. |
| newItem | MenuItem | The item that is highlighted now. |
| oldIndex | int | The previous index. |
| newIndex | int | The new index. |
// Load a preview of whatever the user is currently looking at.menu.OnIndexChange += (_menu, _oldItem, _newItem, _oldIndex, _newIndex) =>{ if (_newItem?.ItemData != null) { PreviewHorse(_newItem.ItemData.model); }};OnMenuOpen
Section titled “OnMenuOpen”Triggered when a menu is opened, either by the user or by a call to Menu.OpenMenu().
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
| menu | Menu | The menu that was opened. |
// Refresh the contents every time the menu is opened.menu.OnMenuOpen += (_menu) =>{ _menu.ClearMenuItems(); BuildItems(_menu);};OnMenuClose
Section titled “OnMenuClose”Triggered when a menu is closed, either by the user or by a call to Menu.CloseMenu(), Menu.GoBack() or MenuController.CloseAllMenus().
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
| menu | Menu | The menu that was closed. |
menu.OnMenuClose += (_menu) =>{ SaveSettings();};Delegate signatures
Section titled “Delegate signatures”If you prefer to declare your handlers explicitly, these are the delegate types the events use. They are all nested in Menu.
| Event | Delegate |
|---|---|
| OnItemSelect | void ItemSelectEvent(Menu menu, MenuItem menuItem, int itemIndex) |
| OnCheckboxChange | void CheckboxItemChangeEvent(Menu menu, MenuCheckboxItem menuItem, int itemIndex, bool newCheckedState) |
| OnListItemSelect | void ListItemSelectedEvent(Menu menu, MenuListItem listItem, int selectedIndex, int itemIndex) |
| OnListIndexChange | void ListItemIndexChangedEvent(Menu menu, MenuListItem listItem, int oldSelectionIndex, int newSelectionIndex, int itemIndex) |
| OnDynamicListItemCurrentItemChange | void DynamicListItemCurrentItemChangedEvent(Menu menu, MenuDynamicListItem dynamicListItem, string oldValue, string newValue) |
| OnDynamicListItemSelect | void DynamicListItemSelectedEvent(Menu menu, MenuDynamicListItem dynamicListItem, string currentItem) |
| OnIndexChange | void IndexChangedEvent(Menu menu, MenuItem oldItem, MenuItem newItem, int oldIndex, int newIndex) |
| OnMenuOpen | void MenuOpenedEvent(Menu menu) |
| OnMenuClose | void MenuClosedEvent(Menu menu) |
