Walkthrough: Adding a Menu to the Visual Studio Menu Bar (C#)

This walkthrough shows how to add a menu to the menu bar of the Visual Studio integrated development environment (IDE). The IDE menu bar contains menu categories such as File, Edit, View, Window, and Help.

By completing this walkthrough, you can create a menu named TestMenu that contains one command. If the command is unavailable, the menu does not appear.

Menus are declared in the .vsct file of the project. For more information about menus and .vsct files, see Commands, Menus, and Toolbars.

Prerequisites

To complete this walkthrough, you must install the Visual Studio 2012 SDK.

Note

For more information about the Visual Studio SDK, see Extending Visual Studio Overview. To find out how to download the Visual Studio SDK, see Visual Studio Extensibility Developer Center on the MSDN Web site.

Locations for the Visual Studio Package Project Template

The Visual Studio Package project template is available in three locations in the New Project dialog box:

  • Under Visual Basic Extensibility. The default language of the project is Visual Basic.

  • Under C# Extensibility. The default language of the project is C#.

  • Under Other Project Types Extensibility. The default language of the project is C++.

Creating a VSPackage

To create the TopLevelMenu VSPackage

  1. Create a VSPackage named TopLevelMenu. For more information, see Walkthrough: Creating a Menu Command By Using the Visual Studio Package Template.

  2. In the Visual Studio Package template, set the programming language to Visual C# or Visual Basic, select Menu Command, set the command name to Test Command, and set command ID to cmdidTestCommand.

Creating a Menu on the IDE Menu Bar

To create a menu

  1. In Solution Explorer, open TopLevelMenu.vsct.

    At the end of the file, there is a Symbols node that contains several GuidSymbol nodes. In the node named "guidTopLevelMenuCmdSet", add a new symbol, as follows:

    <IDSymbol name="TopLevelMenu" value="0x1021"/>
    
  2. Create an empty Menus node in the Commands node, just before Groups.

  3. In the Menus node, create the following Menu node to define the menu registered in step 2:

    <Menu guid="guidTopLevelMenuCmdSet"
    id="TopLevelMenu" priority="0x700" type="Menu">
      <Parent guid="guidSHLMainMenu"
              id="IDG_VS_MM_TOOLSADDINS" />
      <Strings>
        <ButtonText>TestMenu</ButtonText>
        <CommandName>TestMenu</CommandName>
      </Strings>
    </Menu>
    

    The guid and id values of the menu specify the command set and the specific menu in the command set.

    The guid and id values of the parent position the menu on the section of the Visual Studio menu bar that contains the Tools and Add-ins menus.

    The value of the CommandName string specifies that the text should appear in the menu item.

  4. Change the GUID/ID pair of the parent of the generated Group node so that it is the same as that of the menu you created, as follows:

     <Group guid="guidTopLevelMenuCmdSet" id="MyMenuGroup"
    priority="0x0600">
       <Parent guid="guidTopLevelMenuCmdSet" id="TopLevelMenu"/>
     </Group>
    

    This makes the group part of the new menu.

  5. Find the Buttons section. Notice that the Visual Studio Package template has generated a Button element that has its parent set to MyMenuGroup. As a result, this command will appear on your menu.

Building and Testing the TopLevelMenu package

To build and test the VSPackage

  1. Press F5 to open an instance of the Visual Studio experimental environment in debug mode. 

  2. The menu bar in the Visual Studio experimental environment should contain a TestMenu menu that is positioned just before the Analyze menu.

  3. On the TestMenu menu, click Test Command.

    A message box should appear and display the message "Inside Company.TopLevelMenu.TopLevelMenuPackage.MenuItemCallback()". This indicates that the new command works.

See Also

Other Resources

Walkthroughs for Commands, Menus, and Toolbars

Commands, Menus, and Toolbars