Menu.MenuItemCollection.Add Method (String)
Adds a new MenuItem, to the end of the current menu, with a specified caption.
[Visual Basic] Overloads Public Overridable Function Add( _ ByVal caption As String _ ) As MenuItem [C#] public virtual MenuItem Add( string caption ); [C++] public: virtual MenuItem* Add( String* caption ); [JScript] 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.
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.
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 then assigns mainMenu1 to the Menu property of the Form. This example assumes that the code defined in this example is located with in a form.
[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") ' Assign mainMenu1 to the form. Me.Menu = mainMenu1 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"); // Assign mainMenu1 to the form. this.Menu = mainMenu1; } [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"); // Assign mainMenu1 to the form. this->Menu = mainMenu1; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
Menu.MenuItemCollection Class | Menu.MenuItemCollection Members | System.Windows.Forms Namespace | Menu.MenuItemCollection.Add Overload List | Remove