Walkthrough: Adding a Submenu to a Menu (C#)

This walkthrough builds on the demonstration in Walkthrough: Adding a Menu to the Visual Studio Menu Bar (C#) by showing how to add a submenu to the TestMenu menu.

A submenu is a secondary menu that appears in another menu. A submenu can be identified by the arrow that follows its name. Clicking the name causes the submenu and its commands to be displayed.

This walkthrough creates a submenu in a menu on the Visual Studio menu bar and puts a new command on the submenu. The walkthrough also implements the new command.

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

Note

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.

Prerequisites

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.

Creating a VSPackage

To create the TopLevelMenu VSPackage

Adding a Submenu to a Menu

To add a submenu to a menu

  1. In Solution Explorer, double-click TopLevelMenu.vsct.

  2. In the <Symbols> section, add an <IDSymbol> element for the submenu, one for the submenu group, and one for the command, all in the <GuidSymbol> node named "guidTopLevelMenuCmdSet." This is the same node that contains the <IDSymbol> element for the top-level menu.

    <IDSymbol name="SubMenu" value="0x1100"/>
    <IDSymbol name="SubMenuGroup" value="0x1150"/>
    <IDSymbol name="cmdidTestSubCommand" value="0x0105"/>
    
  3. Add the newly created submenu to the <Menus> section.

    <Menu guid="guidTopLevelMenuCmdSet" id="SubMenu" 
          priority="0x0100" type="Menu">
      <Parent guid="guidTopLevelMenuCmdSet" id="MyMenuGroup"/>
      <Strings>
        <ButtonText>Sub Menu</ButtonText>
        <CommandName>Sub Menu</CommandName>
      </Strings>
    </Menu>
    

    The GUID/ID pair of the parent specifies the menu group that was generated in Walkthrough: Adding a Menu to the Visual Studio Menu Bar (C#), and is a child of the top-level menu.

  4. Add the menu group defined in step 2 to the <Groups> section and make it a child of the submenu.

    <Group guid="guidTopLevelMenuCmdSet" id="SubMenuGroup" 
           priority="0x0000">
      <Parent guid="guidTopLevelMenuCmdSet" id="SubMenu"/>
    </Group>
    
  5. Add a new <Button> element to the <Buttons> section to define the command created in step 2 as an item on the submenu.

    <Button guid="guidTopLevelMenuCmdSet" 
            id="cmdidTestSubCommand" priority="0x0000" 
            type="Button">
      <Parent guid="guidTopLevelMenuCmdSet" id="SubMenuGroup" />
      <Icon guid="guidImages" id="bmpPic2" />
      <Strings>
        <CommandName>cmdidTestSubCommand</CommandName>
        <ButtonText>Test Sub Command</ButtonText>
      </Strings>
    </Button>
    

    Note

    In .vsct files, the <Button> element is a generic construct that is used to represent buttons, menu items, and any other user interface (UI) elements that a user can click to execute a command.

  6. In Solution Explorer, right-click TopLevelMenu and then click Rebuild.

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

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

  8. Click TestMenu to see a new submenu named Sub Menu. Click Sub Menu to open the submenu and see a new command, Test Sub Command. Notice that clicking Test Sub Command does nothing.

    Note

    You must close the experimental Visual Studio before you continue to the next step.

Adding a Command

To add support for a command in managed code

  1. Open PkgCmdID.cs in the code editor.

  2. Add the following command ID after the existing command ID in the PkgCmdIDList class definition:

    public const int cmdidTestSubCmd = 0x105;
    
  3. Open TopLevelMenuPackage.cs in the code editor.

  4. Find the hidden region labeled Package Members and expand it by clicking the plus sign in the left margin.

  5. Find the Initialize method and add the following lines just after the call to the AddCommand method.

    CommandID subCommandID = new CommandID(
        GuidList.guidTopLevelMenuCmdSet, 
        (int)PkgCmdIDList.cmdidTestSubCmd);
    MenuCommand subItem = new MenuCommand(
        new EventHandler(SubItemCallback), subCommandID);
    mcs.AddCommand(subItem);
    
  6. In VsPkg.cs, at the end of the class, immediately after the MenuItemCallback method, add the following method. This is the method that is called when the new command in the submenu is clicked.

    private void SubItemCallback(object sender, EventArgs e)
    {
        IVsUIShell uiShell = (IVsUIShell)GetService(
            typeof(SVsUIShell));
        Guid clsid = Guid.Empty;
        int result;
        uiShell.ShowMessageBox(
               0,
               ref clsid,
               "My Top Level Menu Package",
               string.Format(CultureInfo.CurrentCulture, 
               "Inside {0}.SubItemCallback()", 
               this.ToString()),
               string.Empty,
               0,
               OLEMSGBUTTON.OLEMSGBUTTON_OK,
               OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
               OLEMSGICON.OLEMSGICON_INFO,
               0,
               out result);
    }
    
  7. On the Build menu, click Build Solution to build the solution.

  8. Press F5 to open an instance of the experimental Visual Studio.

  9. On the TestMenu menu, click Sub Menu and then click Test Sub Command. A message box should appear and display the text, "Inside Company.TopLevelMenu.TopLevelMenuPackage.SubItemCallback()".

See Also

Tasks

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

Other Resources

Menu and Toolbar Command Walkthroughs

Menus and Toolbars