Walkthrough: Adding a Toolbar to the IDE

This walkthrough shows how to add a toolbar to the Visual Studio integrated development environment (IDE).

A toolbar is a horizontal or vertical strip that contains buttons bound to commands. Depending on its implementation, a toolbar in the IDE can be repositioned, docked on any side of the main IDE window, or made to stay in front of all other windows.

In addition, users can add commands to a toolbar or remove them from it by using the Customize dialog box. Typically, toolbars in VSPackages are user-customizable. The IDE handles all customization, and the VSPackage responds to commands. The VSPackage does not have to know where a command is physically located.

For more information about menus, see Menus and Toolbars.

Prerequisites

Because the product of this walkthrough writes information to the experimental registry hive, Visual Studio 2008 SDK must be installed.

Creating a VSPackage for a Toolbar

This section demonstrates how to use the Visual Studio Integration Package wizard to create a VSPackage that supports a toolbar with a single menu command.

To create the Toolbar VSPackage

  1. Create a VSPackage named IDEToolbar. 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 ToolbarTest Command, and set the command ID to cmdidTestCmd.

Creating a Toolbar for the IDE

To create a toolbar for the IDE

  1. Open Toolbar.vsct in the text editor.

  2. In the Symbols Element, in the GuidSymbol Element named "guidIDEToolbarCmdSet", add declarations for a toolbar and a toolbar group, as follows.

    <IDSymbol name="Toolbar" value="0x1000" />
    <IDSymbol name="ToolbarGroup" value="0x1050" />
    
  3. At the top of the Commands Element, create a Menus Element.

    <Menus></Menus>
    

    The toolbar definition will reside here because the VSCT parser does not distinguish between menus and toolbars at this level.

  4. Add a Menu Element to the <Menus> section to define your toolbar.

    <Menu guid="guidIDEToolbarCmdSet" id="Toolbar"
          type="Toolbar" >
      <CommandFlag>DefaultDocked</CommandFlag>
      <Strings>
        <ButtonText>Test Toolbar</ButtonText>
        <CommandName>Test Toolbar</CommandName>
      </Strings>
    </Menu>
    

    Toolbars cannot be nested like submenus. Therefore, it is not necessary to assign a parent group. Likewise, it is not necessary to set a priority, since toolbars are movable by the user. Typically, initial placement of a toolbar is defined programmatically, but subsequent changes by the user are persisted.

  5. In the Groups Element, after the existing group entry, define a Group Element to contain the commands for the toolbar.

    <Group guid="guidIDEToolbarCmdSet" id="ToolbarGroup"
          priority="0x0000">
      <Parent guid="guidIDEToolbarCmdSet" id="Toolbar"/>
    </Group>
    
  6. In the Buttons Element, change the parent of the existing Button Elementto the toolbar group so that the toolbar will be displayed.

    <Parent guid="guidIDEToolbarCmdSet" id="ToolbarGroup" />
    

    By default, if a toolbar has no commands, it does not appear.

  7. On the Build menu, click Build Solution. Correct any errors that might occur.

  8. Press F5 to open the experimental Visual Studio in debug mode.

  9. Right-click the IDE menu bar, and then click Test Toolbar on the list of toolbars.

  10. Click the icon on the new toolbar to see a dialog box that shows the message "Inside Company.Toolbar.ToolbarPackage.MenuItemCallback()".

See Also

Other Resources

Menu and Toolbar Command Walkthroughs

Menus and Toolbars