Redigera

Dela via


MenuItem Constructors

Definition

Initializes a new instance of the MenuItem class.

Overloads

MenuItem()

Initializes a MenuItem with a blank caption.

MenuItem(String)

Initializes a new instance of the MenuItem class with a specified caption for the menu item.

MenuItem(String, EventHandler)

Initializes a new instance of the class with a specified caption and event handler for the Click event of the menu item.

MenuItem(String, MenuItem[])

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

MenuItem(String, EventHandler, Shortcut)

Initializes a new instance of the class with a specified caption, event handler, and associated shortcut key for the menu item.

MenuItem(MenuMerge, Int32, Shortcut, String, EventHandler, EventHandler, EventHandler, MenuItem[])

Initializes a new instance of the MenuItem class with a specified caption; defined event-handlers for the Click, Select and Popup events; a shortcut key; a merge type; and order specified for the menu item.

MenuItem()

Initializes a MenuItem with a blank caption.

public:
 MenuItem();
public MenuItem ();
Public Sub New ()

Examples

The following code example creates a MenuItem using this version of the constructor.

public:
   void CreateMyMenu()
   {
      // Create an empty menu item object.
      MenuItem^ menuItem1 = gcnew MenuItem;
      // Intialize the menu item using the parameterless version of the constructor.
      // Set the caption of the menu item.
      menuItem1->Text = "&File";
   }
public void CreateMyMenu()
{
   // Create an empty menu item object.
   MenuItem menuItem1 = new MenuItem();
   // Intialize the menu item using the parameterless version of the constructor.
   // Set the caption of the menu item.
   menuItem1.Text = "&File";
}
Public Sub CreateMyMenu()
    ' Create an empty menu item object.
    Dim menuItem1 As New MenuItem()
    ' Intialize the menu item using the parameterless version of the constructor.
    ' Set the caption of the menu item.
    menuItem1.Text = "&File"
End Sub

Remarks

Once you have created a blank MenuItem using this constructor, you can use the properties and methods of the MenuItem class to specify the appearance and behavior of your MenuItem.

Applies to

MenuItem(String)

Initializes a new instance of the MenuItem class with a specified caption for the menu item.

public:
 MenuItem(System::String ^ text);
public MenuItem (string text);
new System.Windows.Forms.MenuItem : string -> System.Windows.Forms.MenuItem
Public Sub New (text As String)

Parameters

text
String

The caption for the menu item.

Examples

The following code example creates a MenuItem that specifies the caption of the menu item at the time it is constructed.

public:
   void CreateMyMenus()
   {
      // Create an instance of a MenuItem with a specified caption.
      menuItem1 = gcnew MenuItem( "&File" );
   }
public void CreateMyMenus()
{
   // Create an instance of a MenuItem with a specified caption.
   menuItem1 = new MenuItem("&File");
}
Public Sub CreateMyMenus()
    ' Create an instance of a MenuItem with a specified caption.
    menuItem1 = New MenuItem("&File")
End Sub

Remarks

When you specify a caption for your menu item with the text parameter, you can also specify an access key by placing an '&' character 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.

Applies to

MenuItem(String, EventHandler)

Initializes a new instance of the class with a specified caption and event handler for the Click event of the menu item.

public:
 MenuItem(System::String ^ text, EventHandler ^ onClick);
public MenuItem (string text, EventHandler onClick);
new System.Windows.Forms.MenuItem : string * EventHandler -> System.Windows.Forms.MenuItem
Public Sub New (text As String, onClick As EventHandler)

Parameters

text
String

The caption for the menu item.

onClick
EventHandler

The EventHandler that handles the Click event for this menu item.

Examples

The following code example creates a MenuItem object with a specified caption and an EventHandler delegate connected to an event handler that will handle the Click event for the menu item.

public:
   void CreateMyMenuItem()
   {
      // Create an instance of MenuItem with caption and an event handler
      MenuItem^ menuItem1 = gcnew MenuItem( "&New",gcnew System::EventHandler(
         this, &Form1::MenuItem1_Click ) );
   }

private:
   // This method is an event handler for menuItem1 to use when connecting its event handler.
   void MenuItem1_Click( Object^ sender, System::EventArgs^ e )
   {
      // Code goes here that handles the Click event.
   }
public void CreateMyMenuItem()
{
   // Create an instance of MenuItem with caption and an event handler
   MenuItem menuItem1 = new MenuItem("&New", new System.EventHandler(this.MenuItem1_Click));
}

// This method is an event handler for menuItem1 to use when connecting its event handler.
private void MenuItem1_Click(Object sender, System.EventArgs e) 
{
   // Code goes here that handles the Click event.
}
Public Sub CreateMyMenuItem()
    ' Create an instance of MenuItem with caption and an event 
    ' handler
    Dim MenuItem1 As New MenuItem("&New", New _
        System.EventHandler(AddressOf Me.MenuItem1_Click))
End Sub
' This method is an event handler for MenuItem1 to use when 
' connecting its event handler.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal _
    e as System.EventArgs)
    ' Code goes here that handles the Click event.
End Sub

Remarks

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.

In addition, you can use this constructor to specify a delegate that will handle the Click event for the menu item being created. The EventHandler that you pass to this constructor must be configured to call an event handler that can handle the Click event. For more information on handling events, see Handling and Raising Events.

Applies to

MenuItem(String, MenuItem[])

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

public:
 MenuItem(System::String ^ text, cli::array <System::Windows::Forms::MenuItem ^> ^ items);
public MenuItem (string text, System.Windows.Forms.MenuItem[] items);
new System.Windows.Forms.MenuItem : string * System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MenuItem
Public Sub New (text As String, items As MenuItem())

Parameters

text
String

The caption for the menu item.

items
MenuItem[]

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

Examples

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:
   void CreateMyMenuItem()
   {
      // submenu item array.
      array<MenuItem^>^ subMenus = gcnew array<MenuItem^>(3);
      // Create three menu items to add to the submenu item array.
      MenuItem^ subMenuItem1 = gcnew MenuItem( "Red" );
      MenuItem^ subMenuItem2 = gcnew MenuItem( "Blue" );
      MenuItem^ subMenuItem3 = gcnew 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.
      MenuItem^ MenuItem1 = gcnew MenuItem( "&Colors",subMenus );
   }
public void CreateMyMenuItem()
{
   // submenu item array.
   MenuItem[] subMenus = new MenuItem[3];
   // Create three menu items to add to the submenu item array.
   MenuItem subMenuItem1 = new MenuItem("Red");
   MenuItem subMenuItem2 = new MenuItem("Blue");
   MenuItem subMenuItem3 = 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.
   MenuItem MenuItem1 = new MenuItem("&Colors", subMenus);
}
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

Remarks

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.

Applies to

MenuItem(String, EventHandler, Shortcut)

Initializes a new instance of the class with a specified caption, event handler, and associated shortcut key for the menu item.

public:
 MenuItem(System::String ^ text, EventHandler ^ onClick, System::Windows::Forms::Shortcut shortcut);
public MenuItem (string text, EventHandler onClick, System.Windows.Forms.Shortcut shortcut);
new System.Windows.Forms.MenuItem : string * EventHandler * System.Windows.Forms.Shortcut -> System.Windows.Forms.MenuItem
Public Sub New (text As String, onClick As EventHandler, shortcut As Shortcut)

Parameters

text
String

The caption for the menu item.

onClick
EventHandler

The EventHandler that handles the Click event for this menu item.

shortcut
Shortcut

One of the Shortcut values.

Examples

The following code example creates an object with a specified caption, shortcut key, and an event handler connected to a method that will handle the event for the menu item.

public:
   void CreateMyMenuItem()
   {
      // Create a MenuItem with caption, shortcut key, and an event handler
      // specified.
      MenuItem^ MenuItem1 = gcnew MenuItem( "&New",
         gcnew System::EventHandler( this, &Form1::MenuItem1_Click ), Shortcut::CtrlL );
   }

private:
   // The following method is an event handler for menuItem1 to use when
   // connecting the event handler.
   void MenuItem1_Click( Object^ sender, EventArgs^ e )
   {
      // Code goes here that handles the Click event.
   }
public void CreateMyMenuItem()
{
   // Create a MenuItem with caption, shortcut key, and an event handler
   // specified.
   MenuItem MenuItem1 = new MenuItem("&New",
       new System.EventHandler(this.MenuItem1_Click), Shortcut.CtrlL);
}

// The following method is an event handler for menuItem1 to use when
// connecting the event handler.
private void MenuItem1_Click(Object sender, EventArgs e)
{
   // Code goes here that handles the Click event.
}
Public Sub CreateMyMenuItem()
    ' Create a MenuItem with caption, shortcut key, and an event handler
    ' specified.
    Dim MenuItem1 As New MenuItem("&New", _
       New System.EventHandler(AddressOf Me.MenuItem1_Click), Shortcut.CtrlL)
End Sub    
   
' The following method is an event handler for menuItem1 to use when
' connecting the event handler.
Private Sub MenuItem1_Click(sender As Object, e As EventArgs)
    ' Code goes here that handles the Click event.
End Sub

Remarks

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. This constructor also enables you to specify a shortcut key in addition to an access key to provide keyboard navigation. Shortcut keys allow you to specify a combination of keys that can be used to activate the menu item.

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

In addition, you can use this constructor to specify a delegate that will handle the Click event for the menu item being created. The EventHandler that you pass to this constructor must be configured to call an event handler that can handle the Click event. For more information on handling events, see Handling and Raising Events.

Applies to

MenuItem(MenuMerge, Int32, Shortcut, String, EventHandler, EventHandler, EventHandler, MenuItem[])

Initializes a new instance of the MenuItem class with a specified caption; defined event-handlers for the Click, Select and Popup events; a shortcut key; a merge type; and order specified for the menu item.

public:
 MenuItem(System::Windows::Forms::MenuMerge mergeType, int mergeOrder, System::Windows::Forms::Shortcut shortcut, System::String ^ text, EventHandler ^ onClick, EventHandler ^ onPopup, EventHandler ^ onSelect, cli::array <System::Windows::Forms::MenuItem ^> ^ items);
public MenuItem (System.Windows.Forms.MenuMerge mergeType, int mergeOrder, System.Windows.Forms.Shortcut shortcut, string text, EventHandler onClick, EventHandler onPopup, EventHandler onSelect, System.Windows.Forms.MenuItem[] items);
new System.Windows.Forms.MenuItem : System.Windows.Forms.MenuMerge * int * System.Windows.Forms.Shortcut * string * EventHandler * EventHandler * EventHandler * System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MenuItem
Public Sub New (mergeType As MenuMerge, mergeOrder As Integer, shortcut As Shortcut, text As String, onClick As EventHandler, onPopup As EventHandler, onSelect As EventHandler, items As MenuItem())

Parameters

mergeType
MenuMerge

One of the MenuMerge values.

mergeOrder
Int32

The relative position that this menu item will take in a merged menu.

shortcut
Shortcut

One of the Shortcut values.

text
String

The caption for the menu item.

onClick
EventHandler

The EventHandler that handles the Click event for this menu item.

onPopup
EventHandler

The EventHandler that handles the Popup event for this menu item.

onSelect
EventHandler

The EventHandler that handles the Select event for this menu item.

items
MenuItem[]

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

Examples

The following code example creates a menu item that has a caption and shortcut key. The menu item also has event handlers defined for the Popup, Click, and Select events. If this menu item is merged, it will add the menu item to the menu with the merge order of zero.

public:
   void CreateMyMenuItem()
   {
      // Submenu item array.
      array<MenuItem^>^ subMenus = gcnew array<MenuItem^>(3);
      // Create three menu items to add to the submenu item array.
      MenuItem^ subMenuItem1 = gcnew MenuItem( "Red" );
      MenuItem^ subMenuItem2 = gcnew MenuItem( "Blue" );
      MenuItem^ subMenuItem3 = gcnew MenuItem( "Green" );
      
      // Add the submenu items to the array.
      subMenus[ 0 ] = subMenuItem1;
      subMenus[ 1 ] = subMenuItem2;
      subMenus[ 2 ] = subMenuItem3;
      /* Create a MenuItem with caption, shortcut key, 
         a Click, Popup, and Select event handler, merge type and order, and an 
         array of submenu items specified.
      */
      MenuItem^ menuItem1 = gcnew MenuItem( MenuMerge::Add, 0,
         Shortcut::CtrlShiftC, "&Colors",
         gcnew EventHandler( this, &Form1::MenuItem1_Click ),
         gcnew EventHandler( this, &Form1::MenuItem1_Popup ),
         gcnew EventHandler( this, &Form1::MenuItem1_Select ), subMenus );
   }

private:
   // The following method is an event handler for menuItem1 to use when connecting the Click event.
   void MenuItem1_Click( Object^ sender, EventArgs^ e )
   {
      // Code goes here that handles the Click event.
   }

   // The following method is an event handler for menuItem1 to use  when connecting the Popup event.
   void MenuItem1_Popup( Object^ sender, EventArgs^ e )
   {
      // Code goes here that handles the Click event.
   }

   // The following method is an event handler for menuItem1 to use  when connecting the Select event
   void MenuItem1_Select( Object^ sender, EventArgs^ e )
   {
      // Code goes here that handles the Click event.
   }
public void CreateMyMenuItem()
{
   // Submenu item array.
   MenuItem[] subMenus = new MenuItem[3];
   // Create three menu items to add to the submenu item array.
   MenuItem subMenuItem1 = new MenuItem("Red");
   MenuItem subMenuItem2 = new MenuItem("Blue");
   MenuItem subMenuItem3 = new MenuItem("Green");

   // Add the submenu items to the array.
   subMenus[0] = subMenuItem1;
   subMenus[1] = subMenuItem2;
   subMenus[2] = subMenuItem3;
   /* Create a MenuItem with caption, shortcut key, 
      a Click, Popup, and Select event handler, merge type and order, and an 
      array of submenu items specified.
   */
   MenuItem menuItem1 = new MenuItem(MenuMerge.Add, 0,
      Shortcut.CtrlShiftC, "&Colors", 
      new EventHandler(this.MenuItem1_Click),
      new EventHandler(this.MenuItem1_Popup),
      new EventHandler(this.MenuItem1_Select), subMenus);
}

// The following method is an event handler for menuItem1 to use when connecting the Click event.
private void MenuItem1_Click(Object sender, EventArgs e)
{
   // Code goes here that handles the Click event.
}

// The following method is an event handler for menuItem1 to use  when connecting the Popup event.
private void MenuItem1_Popup(Object sender, EventArgs e)
{
   // Code goes here that handles the Click event.
}

// The following method is an event handler for menuItem1 to use  when connecting the Select event
private void MenuItem1_Select(Object sender, EventArgs e)
{
   // Code goes here that handles the Click event.
}
Public Sub CreateMyMenuItem()
   ' Submenu item array.
   Dim SubMenus(3) as MenuItem
   ' Create three menu items to add to the submenu item array.
   Dim SubMenuItem1, SubMenuItem2, SubMenuItem3 as MenuItem
   SubMenuItem1 = New MenuItem ("Red")
   SubMenuItem2 = New MenuItem ("Blue")
   SubMenuItem3 = New MenuItem ("Green")
   ' Add the submenu items to the array.
   SubMenus(0) = SubMenuItem1
   SubMenus(1) = SubMenuItem2
   SubMenus(2) = SubMenuItem3
   ' Create 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
   MenuItem1 = New MenuItem(MenuMerge.Add, 0, Shortcut.CtrlShiftC, "&Colors", _
      AddressOf Me.MenuItem1_Click, _
      AddressOf Me.MenuItem1_Popup, _
      AddressOf 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

Remarks

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.

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 item and its submenu items with the menu that it is merging with. The mergeOrder parameter determines where the menu item being created will be positioned when the menu is merged.

In addition, you can use this constructor to create a MenuItem 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 constructor should be configured to call an event handler that can handle the Click event. By using this constructor version, you can also connect the Popup and Select events to determine when this menu item is selected. You can use these events for tasks such as determining whether or not to display a check mark next to submenu items or to enable or disable menu items based on the state of the application. The Select and Click events are raised only for MenuItem objects that are not parent menu items. For more information on handling events, see Handling and Raising Events.

See also

Applies to