Export (0) Print
Expand All

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 create a menu named TestMenu that contains one command. If the command is unavailable, the menu does not appear.

For more information about menus and .vsct files, see Menus and Toolbars.

NoteNote:

Beginning with Visual Studio 2008 SDK, we recommend that you use XML Command Table (.vsct) files instead of command table configuration (.ctc) files to define how menus and commands appear in your VSPackages. For more information, see Visual Studio Command Table (.Vsct) Files.

This walkthrough requires the Visual Studio SDK to be installed. The result of this walkthrough writes information to the experimental registry hive for Visual Studio.

To create the TopLevelMenu VSPackage

  1. Create a VSPackage named TopLevelMenu. For more information, see How to: Create VSPackages (C# and Visual Basic).

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

To create a menu

  1. In Solution Explorer, open TopLevelMenu.vsct.

  2. Register a new menu. At the end of the file, there is a <Symbols> node that contains several <GuidSymbol> nodes. In the node named "guidTopLevelMenuCmdSet", create an <IDSymbol> node, as follows:

    <IDSymbol name="TopLevelMenu" value="0x1021"/>
    

    The name attribute is the ID of the menu and the value is a unique identifier so that collisions can be avoided.

  3. Create an empty <Menus> node in the <Commands> node, just before <Groups>.

  4. In the <Menus> node, create the following <Menu> node to define the menu you 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 place 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.

  5. 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.

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

    <Button guid="guidTopLevelMenuCmdSet" id="cmdidTestCommand" 
            priority="0x0100" type="Button">
      <Parent guid="guidTopLevelMenuCmdSet" id="MyMenuGroup" />
      <Icon guid="guidImages" id="bmpPic1" />
      <Strings>
        <CommandName>cmdidTestCommand</CommandName>
        <ButtonText>Test Command</ButtonText>
      </Strings>
    </Button>
    

To build and test the VSPackage

  1. On the Build menu,click Build Solution.

    Doing this rebuilds the .vsct file with the changes you made. Correct any errors that may occur during building. (The most common error is a GUID label or a command ID that has the wrong case; GUID labels and command IDs are always case-sensitive.)

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

  3. Look at the menu bar in the Visual Studio experimental environment to find TestMenu positioned just before the Analyze menu.

  4. Click TestMenu and then click Test Command.

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

Community Additions

ADD
Show:
© 2014 Microsoft