Επεξεργασία

Κοινή χρήση μέσω


Menu.MenuItemCollection.Add Method

Definition

Adds a new MenuItem to the collection.

Overloads

Add(String)

Adds a new MenuItem, to the end of the current menu, with a specified caption.

Add(MenuItem)

Adds a previously created MenuItem to the end of the current menu.

Add(Int32, MenuItem)

Adds a previously created MenuItem at the specified index within the menu item collection.

Add(String, EventHandler)

Adds a new MenuItem to the end of the current menu with a specified caption and a specified event handler for the Click event.

Add(String, MenuItem[])

Adds a new MenuItem to the end of this menu with the specified caption, Click event handler, and items.

Add(String)

Adds a new MenuItem, to the end of the current menu, with a specified caption.

public:
 virtual System::Windows::Forms::MenuItem ^ Add(System::String ^ caption);
public virtual System.Windows.Forms.MenuItem Add (string caption);
abstract member Add : string -> System.Windows.Forms.MenuItem
override this.Add : string -> System.Windows.Forms.MenuItem
Public Overridable Function Add (caption As String) As MenuItem

Parameters

caption
String

The caption of the menu item.

Returns

A MenuItem that represents the menu item being added to the collection.

Examples

The following code example uses the derived class MainMenu to create a main menu, mainMenu1, that has two MenuItem objects added to its MenuItems collection. The code then assigns mainMenu1 to the Menu property of the Form. This example requires that the code defined in this example is located with in a form.

private:
   void InitializeMyMainMenu()
   {
      // Create the MainMenu.
      MainMenu^ mainMenu1 = gcnew MainMenu;
      
      /* Use the MenuItems property to call the Add method
         to add two new MenuItem objects to the MainMenu. */
      mainMenu1->MenuItems->Add( "&File" );
      mainMenu1->MenuItems->Add( "&Edit" );
      
      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }
private void InitializeMyMainMenu()
{
   // Create the MainMenu.
   MainMenu mainMenu1 = new MainMenu();
   
   /* Use the MenuItems property to call the Add method
      to add two new MenuItem objects to the MainMenu. */
   mainMenu1.MenuItems.Add ("&File");
   mainMenu1.MenuItems.Add ("&Edit");

   // Assign mainMenu1 to the form.
   this.Menu = mainMenu1;
}
Private Sub InitializeMyMainMenu()
    ' Create the MainMenu.
    Dim mainMenu1 As New MainMenu()
       
    ' Use the MenuItems property to call the Add method
    ' to add two new MenuItem objects to the MainMenu. 
    mainMenu1.MenuItems.Add("&File")
    mainMenu1.MenuItems.Add("&Edit")
       
    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1
End Sub

Remarks

A MenuItem can only be contained in one menu at a time, and cannot be added more than once to the same menu. To reuse a MenuItem in more than one menu, use the CloneMenu method of the MenuItem class. To remove a MenuItem that you have previously added, use the Remove method.

See also

Applies to

Add(MenuItem)

Adds a previously created MenuItem to the end of the current menu.

public:
 virtual int Add(System::Windows::Forms::MenuItem ^ item);
public virtual int Add (System.Windows.Forms.MenuItem item);
abstract member Add : System.Windows.Forms.MenuItem -> int
override this.Add : System.Windows.Forms.MenuItem -> int
Public Overridable Function Add (item As MenuItem) As Integer

Parameters

item
MenuItem

The MenuItem to add.

Returns

The zero-based index where the item is stored in the collection.

Examples

The following code example creates an instance of the derived class, MainMenu, and adds a MenuItem to its collection of MenuItem objects. This example requires that the method defined in this example is located within the class for a form and called by a method in that form class.

private:
   void InitializeMyMainMenu()
   {
      // Create the MainMenu and the MenuItem to add.
      MainMenu^ mainMenu1 = gcnew MainMenu;
      MenuItem^ menuItem1 = gcnew MenuItem( "&File" );
      
      /* Use the MenuItems property to call the Add method
         to add the MenuItem to the MainMenu menu item collection. */
      mainMenu1->MenuItems->Add( menuItem1 );
      
      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }
private void InitializeMyMainMenu()
{
   // Create the MainMenu and the MenuItem to add.
   MainMenu mainMenu1 = new MainMenu();
   MenuItem menuItem1 = new MenuItem("&File");
   
   /* Use the MenuItems property to call the Add method
      to add the MenuItem to the MainMenu menu item collection. */
   mainMenu1.MenuItems.Add (menuItem1);

   // Assign mainMenu1 to the form.
   this.Menu = mainMenu1;
}
Private Sub InitializeMyMainMenu()
    ' Create the MainMenu and the MenuItem to add.
    Dim mainMenu1 As New MainMenu()
    Dim menuItem1 As New MenuItem("&File")
       
    ' Use the MenuItems property to call the Add method
    ' to add the MenuItem to the MainMenu menu item collection. 
    mainMenu1.MenuItems.Add(menuItem1)
       
    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1
End Sub

Remarks

A MenuItem can only be contained in one menu at a time, and cannot be added more than once to the same menu. To reuse a MenuItem in more than one menu, use the CloneMenu method of the MenuItem class. To remove a MenuItem that you have previously added, use the Remove method.

This version of the Add method allows you to add previously created MenuItem objects to the end of the menu item collection.

See also

Applies to

Add(Int32, MenuItem)

Adds a previously created MenuItem at the specified index within the menu item collection.

public:
 virtual int Add(int index, System::Windows::Forms::MenuItem ^ item);
public virtual int Add (int index, System.Windows.Forms.MenuItem item);
abstract member Add : int * System.Windows.Forms.MenuItem -> int
override this.Add : int * System.Windows.Forms.MenuItem -> int
Public Overridable Function Add (index As Integer, item As MenuItem) As Integer

Parameters

index
Int32

The position to add the new item.

item
MenuItem

The MenuItem to add.

Returns

The zero-based index where the item is stored in the collection.

Exceptions

The MenuItem being added is already in use.

The index supplied in the index parameter is larger than the size of the collection.

Examples

The following code example creates an instance of the derived class, MainMenu, and adds a MenuItem object to its collection of MenuItem objects at a specific location in the menu item collection. This example requires that the method defined in this example is located within the class for a form and called by a method in that form class.

private:
   void InitializeMyMainMenu()
   {
      // Create the MainMenu and the MenuItem to add.
      MainMenu^ mainMenu1 = gcnew MainMenu;
      MenuItem^ menuItem1 = gcnew MenuItem( "&File" );
      
      /* Use the MenuItems property to call the Add method
         to add the MenuItem to mainMenu1 at specific index. */
      mainMenu1->MenuItems->Add( 0, menuItem1 );
      
      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }
private void InitializeMyMainMenu()
{
   // Create the MainMenu and the MenuItem to add.
   MainMenu mainMenu1 = new MainMenu();
   MenuItem menuItem1 = new MenuItem("&File");
   
   /* Use the MenuItems property to call the Add method
      to add the MenuItem to mainMenu1 at specific index. */
   mainMenu1.MenuItems.Add (0, menuItem1);

   // Assign mainMenu1 to the form.
   this.Menu = mainMenu1;
}
Private Sub InitializeMyMainMenu()
    ' Create the MainMenu and the MenuItem to add.
    Dim mainMenu1 As New MainMenu()
    Dim menuItem1 As New MenuItem("&File")
       
    ' Use the MenuItems property to call the Add method
    ' to add the MenuItem to mainMenu1 at specific index. 
    mainMenu1.MenuItems.Add(0, menuItem1)
       
    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1
End Sub

Remarks

A MenuItem can only be contained in one menu at a time, and cannot be added more than once to the same menu. To reuse a MenuItem in more than one menu, use the CloneMenu method of the MenuItem class. To remove a MenuItem that you have previously added, use the Remove method.

This version of the Add method allows you to add previously created MenuItem objects to a specific index location within the collection. Any MenuItem currently located at that index, and all MenuItem objects after that index, are moved to the next lowest index in the collection.

See also

Applies to

Add(String, EventHandler)

Adds a new MenuItem to the end of the current menu with a specified caption and a specified event handler for the Click event.

public:
 virtual System::Windows::Forms::MenuItem ^ Add(System::String ^ caption, EventHandler ^ onClick);
public virtual System.Windows.Forms.MenuItem Add (string caption, EventHandler onClick);
abstract member Add : string * EventHandler -> System.Windows.Forms.MenuItem
override this.Add : string * EventHandler -> System.Windows.Forms.MenuItem
Public Overridable Function Add (caption As String, onClick As EventHandler) As MenuItem

Parameters

caption
String

The caption of the menu item.

onClick
EventHandler

An EventHandler that represents the event handler that is called when the item is clicked by the user, or when a user presses an accelerator or shortcut key for the menu item.

Returns

A MenuItem that represents the menu item being added to the collection.

Examples

The following code example uses the derived class MainMenu to create a main menu, mainMenu1, that has two MenuItem objects added to its MenuItems collection. The code uses this version of the Add method to define an event handler for the Click event of the second menu item added to the collection. The code then assigns mainMenu1 to the Menu property of the Form. This example requires that the code defined in this example is located within a form.

private:
   void InitializeMyMainMenu()
   {
      // Create the MainMenu.
      MainMenu^ mainMenu1 = gcnew MainMenu;
      
      /* Use the MenuItems property to call the Add method
         to add two new MenuItem objects to the MainMenu. */
      mainMenu1->MenuItems->Add( "&File" );
      mainMenu1->MenuItems->Add( "&Edit", gcnew EventHandler(
         this, &Form1::menuItem2_Click ) );
      
      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }

private:
   void menuItem2_Click( System::Object^ sender, System::EventArgs^ e )
   {
      // Insert code to handle Click event.
   }
private void InitializeMyMainMenu()
{
   // Create the MainMenu.
   MainMenu mainMenu1 = new MainMenu();
   
   /* Use the MenuItems property to call the Add method
      to add two new MenuItem objects to the MainMenu. */
   mainMenu1.MenuItems.Add ("&File");
   mainMenu1.MenuItems.Add ("&Edit", new EventHandler (menuItem2_Click));

   // Assign mainMenu1 to the form.
   this.Menu = mainMenu1;
}

private void menuItem2_Click(System.Object sender, System.EventArgs e)
{
   // Insert code to handle Click event.
}
Private Sub InitializeMyMainMenu()
    ' Create the MainMenu.
    Dim mainMenu1 As New MainMenu()
       
    ' Use the MenuItems property to call the Add method
    ' to add two new MenuItem objects to the MainMenu. 
    mainMenu1.MenuItems.Add("&File")
    mainMenu1.MenuItems.Add("&Edit", _
       New EventHandler(AddressOf menuItem2_Click))
       
    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1
End Sub    
   
Private Sub menuItem2_Click(sender As System.Object, e As System.EventArgs)
    ' Insert code to handle Click event.
End Sub

Remarks

A MenuItem can only be contained in one menu at a time, and cannot be added more than once to the same menu. To reuse a MenuItem in more than one menu, use the CloneMenu method of the MenuItem class. To remove a MenuItem that you have previously added, use the Remove method.

This version of the Add method allows you to specify a caption for the menu item and a delegate to handle the Click event. You can use this version of the Add method if your application already has an event handler to handle the Click event.

Note

The Click event is not raised for a MenuItem that contains submenu items.

See also

Applies to

Add(String, MenuItem[])

Adds a new MenuItem to the end of this menu with the specified caption, Click event handler, and items.

public:
 virtual System::Windows::Forms::MenuItem ^ Add(System::String ^ caption, cli::array <System::Windows::Forms::MenuItem ^> ^ items);
public virtual System.Windows.Forms.MenuItem Add (string caption, System.Windows.Forms.MenuItem[] items);
abstract member Add : string * System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MenuItem
override this.Add : string * System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MenuItem
Public Overridable Function Add (caption As String, items As MenuItem()) As MenuItem

Parameters

caption
String

The caption of the menu item.

items
MenuItem[]

An array of MenuItem objects that this MenuItem will contain.

Returns

A MenuItem that represents the menu item being added to the collection.

Remarks

A MenuItem can only be contained in one menu at a time, and cannot be added more than once to the same menu. To reuse a MenuItem in more than one menu, use the CloneMenu method of the MenuItem class. To remove a MenuItem that you have previously added, use the Remove method.

This version of the Add method allows you to specify a caption for the menu item and a delegate that will handle its Click event. You can use this version of the Add method if your application already has an event handler to handle the Click event. This version of the method also allows you to specify an array of previously created MenuItem objects that you want to add to the collection. You can use this feature to reuse existing MenuItem objects that have been cloned using the CloneMenu method. If the items parameter is not empty or null, the MenuItem being added to the collection will contain submenu items.

Note

The Click event is not raised for a MenuItem that contains submenu items.

See also

Applies to