Initializes a new instance of the class with a specified caption, defined event-handlers for the System.MenuItem.Click, System.MenuItem.Select and System.WinForms.MenuItem.Popup events, a shortcut key, a merge type, and order specified for the menu item.
[Visual Basic] Overloads Public Sub New( _ ByVal mergeType As MenuMerge, _ ByVal mergeOrder As Integer, _ ByVal shortcut As Shortcut, _ ByVal text As String, _ ByVal onClick As EventHandler, _ ByVal onPopup As EventHandler, _ ByVal onSelect As EventHandler, _ ByVal items() As MenuItem _ ) [C#] public MenuItem( MenuMerge mergeType, int mergeOrder, Shortcut shortcut, string text, EventHandler onClick, EventHandler onPopup, EventHandler onSelect, MenuItem[] items ); [C++] public: MenuItem( MenuMerge mergeType, int mergeOrder, Shortcut shortcut, String* text, EventHandler* onClick, EventHandler* onPopup, EventHandler* onSelect, MenuItem* items[] ); [JScript] public function MenuItem( mergeType : MenuMerge, mergeOrder : int, shortcut : Shortcut, text : String, onClick : EventHandler, onPopup : EventHandler, onSelect : EventHandler, items : MenuItem[] );
When you specify a caption for your menu item with the text parameter, you can also specify an accelerator key by placing an '&' before the character to be used as the accelerator key. For example, to specify the "F" in "File" as an accelerator key, you would specify the caption for the menu item as "&File".
The items parameter allows you to assign an array of items to define a submenu of this menu item. Each item in the array can also have an array of menu items assigned to it. Since each menu item can have any number of menu items assigned to it, you can very easily create complete menu structures.
The mergeType and mergeOrder parameters allow you to determine how this menu item will behave when the menu item is merged with another menu. Depending on the value you specify for the mergeType parameter, you can either add, remove, replace, or merge the menu items with the menu it is merging with.
In addition, you can use this constructor to create a and have it connected to an event handler in your code that will process the click of the menu item. The EventHandler that you pass into this contructor should be configured to call an event handler that can handle the System.Click event. For more information on handling events see, TBD.
By using this contructor version, you can also connect the System.WinForms.MenuItem.Popup and System.WinForms.MenuItem.Select events to determine when this menu item is selected. You can use these events to determine whether or not to display a checkmark next to submenu items and perform other menu management operations.
The following example creates a menu item that has a caption and shortcut key. The menu item also has event handlers defined for the System.WinForms.MenuItem.Popup, System.WinForms.MenuItem.Click, and System.WinForms.MenuItem.Select events. If this menu item is merged, it will add the menu item to the menu with the merge order of zero.
[Visual Basic]
Public Sub CreateMyMenuItem() ' Sub menu item array. Dim SubMenus(3) as MenuItem ' Create three menu items to add to the submenu item array. Dim SubMenuItem1, SubMenuItem2, SubMenuItem3 as MenuItem Set SubMenuItem1 = New MenuItem ("Red") Set SubMenuItem2 = New MenuItem ("Blue") Set SubMenuItem3 = New MenuItem ("Green") ' Add the submenu items to the array. Set SubMenus(1) = SubMenuItem1 Set SubMenus(2) = SubMenuItem2 Set SubMenus(3) = SubMenuItem3 'Create an instance of a MenuItem with caption, shortcut key, a Click, Popup, and Select event handler, menu merge type and order, and an array of submenu items specified. Dim MenuItem1 As MenuItem Set MenuItem1 = New MenuItem(MenuMerge.Add, 0, "&Colors", _ New System.EventHandler(AdressOf Me.MenuItem1_Click), _ New System.EventHandler(AdressOf Me.MenuItem1_Popup), _ New System.EventHandler(AdressOf Me.MenuItem1_Select), SubMenus) End Sub ' The following method is an event handler for MenuItem1 to use when connecting the Click event. Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e as System.EventArgs) ' Code goes here that handles the Click event. End Sub ' The following method is an event handler for MenuItem1 to use when connecting the Popup event. Private Sub MenuItem1_Popup(ByVal sender As System.Object, ByVal e as System.EventArgs) ' Code goes here that handles the Click event. End Sub ' The following method is an event handler for MenuItem1 to use when connecting the Select event Private Sub MenuItem1_Select(ByVal sender As System.Object, ByVal e as System.EventArgs) ' Code goes here that handles the Click event. End Sub
MenuItem Class | MenuItem Members | System.WinForms Namespace | MenuItem Constructor Overload List