Export (0) Print
Expand All

Adding Menus and Menu Items to Windows Forms

Visual Studio .NET 2003

A menu on a Windows Form is created with a MainMenu object, which is a collection of MenuItem objects. You can add menus to Windows Forms at design time by adding the MainMenu component and then appending menu items to it using the Menu Designer. Menus can also be added programmatically by adding one or more MainMenu objects to a Windows Form and adding MenuItem objects to the collection. The procedures in this topic show how to create a simple menu named File, either with the Menu Designer or in code.

To add a menu to a Windows Form at design time

  1. Open the form you wish to add a menu to in the Windows Forms Designer.
  2. In the Toolbox, double-click the MainMenu component.

    A menu is added to the form (displaying the text "Type Here"), and the MainMenu component is added to the component tray.

To add a menu to a Windows Form programmatically

  1. In the Code Editor, create a new public method that includes the code to add the menu to your form. For more information on opening the Code Editor, see Displaying Windows Forms in the Designer.
    ' Visual Basic
    Public Sub AddMenu()
    
    End Sub
    
    // C#
    public void AddMenu(){
    
    }
    
    // C++
    public:
       void AddMenu()
       {
    
       }
    
  2. Add code within the method similar to the following to create an instance of the MainMenu component:
    ' Visual Basic
       Dim mnuFileMenu as New MainMenu()
       Me.Menu = mnuFileMenu
    
    // C#
       MainMenu mnuFileMenu = new MainMenu();
       this.Menu = mnuFileMenu;
    
    // C++
       MainMenu *mnuFileMenu = new MainMenu();
       this->Menu = mnuFileMenu;
    

After you have added a MainMenu component to your Windows Form, you need to add menu items to it. The Menu Designer allows you to add menu items to your menu component at design time. Menu contents are kept within a collection, so you can add menu items to a menu at run time by adding MenuItem objects to this collection.

To add menu items to a menu at design time

  1. Click the MainMenu component on the Windows Form.

    The text "Type Here" is displayed.

  2. Click the displayed text, and type the name of the desired menu item to add it.
  3. To add another menu item, click another "Type Here" area within the Menu Designer.
    • Click the area to the right of the current menu item to add a submenu.

      -or-

    • Click the area below the current menu item to add another entry to the same menu.

To add menu items to a menu programmatically

  1. If there is not one already, add a MainMenu component to the Windows Form within a public method, and assign an instance of the MainMenu class to the Menu property of the form. For more information about collections, see Relation of Objects to Each Other.
    ' Visual Basic
    Public Sub AddMenuAndItems()
       Dim mnuFileMenu as New MainMenu()
       Me.Menu = mnuFileMenu
    End Sub
    
    // C#
    public void AddMenuAndItems(){
       MainMenu mnuFileMenu = new MainMenu();
       this.Menu = mnuFileMenu;
    }
    
    // C++
    public:
       void AddMenu()
       {
          MainMenu *mnuFileMenu = new MainMenu();
          this->Menu = mnuFileMenu;
       }
    
  2. Within the method, create MenuItem objects to add to the MainMenu object's collection:
    ' Visual Basic
       Dim myMenuItemFile as New MenuItem("&File")
       Dim myMenuItemNew as New MenuItem("&New")
    
    // C#
       MenuItem myMenuItemFile = new MenuItem("&File");
       MenuItem myMenuItemNew = new MenuItem("&New");
    
    // C++
       MenuItem *myMenuItemFile = new MenuItem(S"&File");
       MenuItem *myMenuItemNew = new MenuItem(S"&New");
    
    Note   By default, a MainMenu object contains no menu items, so that the first menu item added becomes the menu heading. This is why the Text property of
    myMenuItemFile
    is set to
    &File
    .
  3. Within the method, assign the top-level menu item and add subsequent menu items to it:
    ' Visual Basic
       mnuFileMenu.MenuItems.Add(myMenuItemFile)
       myMenuItemFile.MenuItems.Add(myMenuItemNew)
    
    // C#
       mnuFileMenu.MenuItems.Add(myMenuItemFile);
       myMenuItemFile.MenuItems.Add(myMenuItemNew);
    
    // C++
       mnuFileMenu->MenuItems->Add(myMenuItemFile);
       myMenuItemFile->MenuItems->Add(myMenuItemNew);
    
    Note   To create submenus, you can add MenuItem objects to the MenuItems property of the parent MenuItem. From the example above, if you wanted to add a third menu item (
    myMenuItemFolder
    ) as a submenu of the second menu item (
    myMenuItemNew
    ), you would include the following lines of code:
    ' Visual Basic
    Dim myMenuItemFolder as New MenuItem("&Folder")
    myMenuItemNew.MenuItems.Add(myMenuItemFolder)
    
    // C#
    MenuItem myMenuItemFolder = new MenuItem("&Folder");
    myMenuItemNew.MenuItems.Add(myMenuItemFolder);
    
    // C++
    MenuItem *myMenuItemFolder = new MenuItem(S"&Folder");
    myMenuItemNew->MenuItems->Add(myMenuItemFolder);
    

    Menu items can also be dynamically added when they are created, such that properties are set at the time of their creation and addition. For example, to add a fourth menu item to the collection above, enter the following code:

    ' Visual Basic
    mnuFileMenu.MenuItems.Add("Save &As")
    
    // C#
    mnuFileMenu.MenuItems.Add("Save &As");
    
    // C++
    mnuFileMenu->MenuItems->Add("Save &As");
    

For more information about dynamically creating menu items when they are added to a MenuItems collection, see Menu.MenuItemCollection Class.

Add Functionality to the Menu Items

Finally, once your menu structure is established, you will want to provide functionality for the menu items. This is often achieved through and event handler of the MenuItem.Click event. For more information on event handlers, see Introduction to Event Handlers in Windows Forms.

See Also

Introduction to the Windows Forms MainMenu Component | Menus in Windows Forms | What's New in Windows Forms and Controls

Show:
© 2014 Microsoft