Menu.MenuItemCollection.Add Method (String, EventHandler)
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
'Declaration Public Overridable Function Add ( _ caption As String, _ onClick As EventHandler _ ) As MenuItem
- Type: System.String
The caption of the menu item.
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.
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 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
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.