Freigeben über


Walkthrough: Adding a Submenu to a Menu

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.

Prerequisites

To complete this walkthrough, you must install the Visual Studio 2010 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

Adding a Submenu to a Menu

To add a submenu to a menu

  1. In Solution Explorer, open 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.vsct 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 section.

Adding a Command

To add support for a command in managed code

  1. Open PkgCmdID.cs or PkgCmdID.vb 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 or TopLevelMenuPackage.vb 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. The symbol SubItemCallback will be undefined until after step 6.

                CommandID subCommandID = new CommandID(
    GuidList.guidTopLevelMenuCmdSet,
    (int)PkgCmdIDList.cmdidTestSubCmd);
                MenuCommand subItem = new MenuCommand(
                    new EventHandler(SubItemCallback), subCommandID);
                mcs.AddCommand(subItem);
    
  6. 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

Walkthroughs for Commands, Menus, and Toolbars

Commands, Menus, and Toolbars