Export (0) Print
Expand All

Menu.MenuItemCollection.Add Method (String)

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public virtual MenuItem Add (
	string caption
)
public MenuItem Add (
	String caption
)
public function Add (
	caption : String
) : MenuItem

Parameters

caption

The caption of the menu item.

Return Value

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

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.

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 = 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;
}
   

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft