Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Menu.MenuItemCollection.Add Method

Adds a new MenuItem to the collection.

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Add(MenuItem) As Integer
[C#] public virtual int Add(MenuItem);
[C++] public: virtual int Add(MenuItem*);
[JScript] public function Add(MenuItem) : int;

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

[Visual Basic] Overloads Public Overridable Function Add(String) As MenuItem
[C#] public virtual MenuItem Add(string);
[C++] public: virtual MenuItem* Add(String*);
[JScript] public function Add(String) : MenuItem;

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

[Visual Basic] Overloads Public Overridable Function Add(Integer, MenuItem) As Integer
[C#] public virtual int Add(int, MenuItem);
[C++] public: virtual int Add(int, MenuItem*);
[JScript] public function Add(int, MenuItem) : int;

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

[Visual Basic] Overloads Public Overridable Function Add(String, EventHandler) As MenuItem
[C#] public virtual MenuItem Add(string, EventHandler);
[C++] public: virtual MenuItem* Add(String*, EventHandler*);
[JScript] public function Add(String, EventHandler) : MenuItem;

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

[Visual Basic] Overloads Public Overridable Function Add(String, MenuItem()) As MenuItem
[C#] public virtual MenuItem Add(string, MenuItem[]);
[C++] public: virtual MenuItem* Add(String*, MenuItem[]);
[JScript] public function Add(String, MenuItem[]) : MenuItem;

Example

[Visual Basic, C#, C++] The following 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 assumes that the code defined in this example is located within a form.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Add. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
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    
   
Protected Sub menuItem2_Click(sender As System.Object, e As System.EventArgs)
    ' Insert code to handle Click event.
End Sub


[C#] 
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;
}

protected void menuItem2_Click(System.Object sender, System.EventArgs e)
{
   // Insert code to handle Click event.
}


[C++] 
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 (S"&File");
    mainMenu1->MenuItems->Add (S"&Edit", new EventHandler (this, &Form1::menuItem2_Click));
 
    // Assign mainMenu1 to the form.
    this->Menu = mainMenu1;
 }
 
protected:
 void menuItem2_Click(System::Object* /*sender*/, System::EventArgs* /*e*/)
 {
    // Insert code to handle Click event.
 }
 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Menu.MenuItemCollection Class | Menu.MenuItemCollection Members | System.Windows.Forms Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.