Represents an individual item that is displayed within a Menu or Menu.
Object
MarshalByRefObject
MarshalByRefComponent
Menu
MenuItem
[Visual Basic] Public Class MenuItem Inherits Menu [C#] public class MenuItem : Menu [C++] public __gc class MenuItem : public Menu [JScript] public class MenuItem extends Menu
In order for a MenuItem to be displayed, you must add it to a MainMenu or ContextMenu. To create sub menus, you can add MenuItem objects to the ContextMenu property of the parent MenuItem.
The MenuItem class provides properties that allow you to configure the appearance and functionality of a menu item. To display a checkmark next to a menu item, use the Checked property. You can use the Shortcut property to define a keyboard combination that can be pressed to select the menu item.
The System.WinForms.MenuItem.Popup event allows you to perform tasks before a menu is displayed. You can create an event handler for this event to display or hide menu items based on the state of your code.
Namespace: System.WinForms
Assembly: System.WinForms.dll
The following example creates a object, adds a MenuItem to represent the top-level menu item, adds a sub menu item to it for selecting a font size, and then adds two sub menu items to that menu item that represent large and small font choices in an application. The example assumes that there is a MainMenu object named MainMenu1 and four MainMenu objects named MenuItem1, MenuItem2, MenuItem3, and MenuItem4.
[Visual Basic]
Public Sub CreateMyMenu() ' Set the caption for the top-level menu item. MenuItem1.Text = "Edit" ' Set the caption for the first sub menu. MenuItem2.Text = "Font Size" ' Set the caption for MenuItem2's first sub menu. MenuItem3.Text = "Small" ' Set the checked property to True since this is the default value. MenuItem3.Checked = True ' Define a shortcut key combination for the menu item. MenuItem3.Shortcut = Shortcut.CtrlS ' Set the caption of the second sub menu item of MenuItem2. MenuItem4.Text = "Large" ' Define a shortcut key combination for the menu item. MenuItem4.Shortcut = Shortcut.CtrlL ' Set the index of the menu item so it is placed below the first sub menu item. MenuItem4.Index = 1 ' Create a MenuItem array and store the first sub menu item. Dim tempMenu1(0) As System.WinForms.MenuItem Set tempMenu1(0) = MenuItem2 ' Assign the MenuItem array to MenuItem1's collection of menu items. MenuItem1.MenuItems.All = tempMenu1 ' Create a MenuItem array and store the sub menus MenuItem3 and MenuItem4. Dim tempMenu2(1) As System.WinForms.MenuItem Set tempMenu2(0) = MenuItem3 Set tempMenu2(1) = MenuItem4 ' Assign the Menuitem array to MenuItem2's collection of menu items. MenuItem2.MenuItems.All = tempMenu2 ' Create a MenuItem array to store all the top level menu items to display in MainMenu1. Dim tempMenu3(0) As System.WinForms.MenuItem Set tempMenu3(0) = MenuItem1 ' Add the entire menu structure to MainMenu1's collection of menu items to display. MainMenu1.MenuItems.All = tempMenu3 End Sub
MenuItem Members | System.WinForms Namespace | MainMenu | Menu | MenuMerge