Export (0) Print
Expand All

MenuItem Constructor (String, MenuItem())

Initializes a new instance of the class with a specified caption and an array of submenu items defined for the menu item.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Sub New ( _
	text As String, _
	items As MenuItem() _
)

Parameters

text
Type: System.String

The caption for the menu item.

items
Type: System.Windows.Forms.MenuItem()

An array of MenuItem objects that contains the submenu items for this menu item.

When you specify a caption for your menu item with the text parameter, you can also specify an access key by placing an '&' before the character to be used as the access key. For example, to specify the "F" in "File" as an access key, you would specify the caption for the menu item as "&File". You can use this feature to provide keyboard navigation for your menus.

Setting the text parameter to "-" causes your menu item to be displayed as a separator (a horizontal line) rather than a standard menu item.

The items parameter enables you to assign an array of menu 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. This enables you to create complete menu structures and assign them to the constructor for the menu item.

For more information on handling events, see Handling and Raising Events.

The following code example creates an object with a specified caption, an event handler connected to a method that will handle the event each menu item in an array of submenu items.

Public Sub CreateMyMenuItem()
    ' submenu item array. 
    Dim subMenus(3) As MenuItem
    ' Create three menu items to add to the submenu item array. 
    Dim subMenuItem1 As New MenuItem("Red")
    Dim subMenuItem2 As New MenuItem("Blue")
    Dim subMenuItem3 As New MenuItem("Green")
    ' Add the submenu items to the array.
    subMenus(0) = subMenuItem1
    subMenus(1) = subMenuItem2
    subMenus(2) = subMenuItem3
    ' Create an instance of a MenuItem with caption and an array of submenu 
    ' items specified. 
    Dim MenuItem1 As New MenuItem("&Colors", subMenus)
End Sub

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft