This documentation is archived and is not being maintained.

Walkthrough: Adding a Command to the Solution Explorer Toolbar (C#)

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

This walkthrough details the steps required to add a button to the Visual Studio Solution Explorer toolbar.

Any command, whether a toolbar button or a menu item, is considered a button by Visual Studio. When the button is clicked the code contained in the command handler is executed. Usually, related commands are grouped together to form a single group. Menus or toolbars act as containers for groups. Priority determines the order in which individual commands within a group appear in the menu or on the toolbar. You can prevent the button from being displayed on the toolbar or the menu by controlling its visibility. A command listed in the VISIBILITY section of the .ctc file appears only with the associated context. The visibility cannot be applied to groups.

For more information on menus, toolbar commands and .ctc file, see Menus and Toolbars.

This walkthrough requires installation of the Visual Studio Industry Partner (VSIP) SDK. The result of this walkthrough writes information to the experimental registry hive for Visual Studio.

This section of the walkthrough demonstrates how to create a VSPackage that supports a single menu command using the Visual Studio Integration Package wizard.

To create the MySolutionToolbarPkg VSPackage

  1. Open Visual Studio. On the File menu, point to New and select Project.

  2. In the New Project dialog box, select Other Project Types and then select Extensibility from the Project types list.

  3. In the Templates list, select Visual Studio Integration Package.

  4. Type in the name of your project; for example, MySolutionToolbarPkg. The rest of this walkthrough assumes that name.

  5. Click OK to open the Visual Studio Integration Package Wizard.

  6. Click the Next button on the Welcome to the Visual Studio Integration Package Wizard page.

  7. In the Select a Programming Language page, select Visual C# and accept the default for the key file, and then click Next.

  8. In the Basic VSPackage Information page, you can change whatever details you want. For this example, however, you can just accept the defaults and click Next.

  9. In the Select VSPackage Options page, select Menu Command and then click Next.

  10. In the Command Options page, set the Command Name to My Button Test Command and set the Command ID as cmdidMyTestCmd. Click Finish.

  11. After a few moments, the Package wizard finishes creating the project. Proceed to steps for adding a button to the Solution Explorer toolbar.

This section of the walkthrough demonstrates how to add a single button to the Solution Explorer toolbar. You then click the button to execute the code contained in your callback method.

To add a button to the Solution Explorer toolbar

  1. In Solution Explorer, expand the CtcComponents folder in the MySolutionToolbarPkg project to find the CommandIds.h file. Right-click CommandIds.h and select Open to open it in a text editor.

  2. In the Menu Group IDs section, add a definition for MySolutionToolbarGroup:

    #define MySolutionToolbarGroup     0x1090
    
  3. Click the Save button and then close the CommandIds.h file.

  4. Also in the CtcComponents folder in Solution Explorer, right click MySolutionToolbarPkg.ctc and select Open to open it in a text editor.

  5. In the NEWGROUPS_BEGIN section, add the following line after the existing group entry. This defines the new group containing commands for the Solutions Explorer toolbar.

    // Any command added to this group appears on the toolbar.
    guidMySolutionToolbarPkgCmdSet:MySolutionToolbarGroup, // Group ID
    guidSHLMainMenu:IDM_VS_TOOL_PROJWIN,                   // Toolbar ID
    0xF000;                                                // Priority
    
  6. In the CMDPLACEMENT_SECTION section, add the following line. This adds the existing command My Button Test Command created in the previous section to the toolbar group. By default, the toolbar does not appear if it has no commands associated with it.

    guidMySolutionToolbarPkgCmdSet:cmdidMyTestCmd,         // Command ID
    guidMySolutionToolbarPkgCmdSet:MySolutionToolbarGroup, // Parent ID
    0xF000;                                                // Priority
    
  7. Click the Save button and then close the MySolutionToolbarPkg.ctc file.

  8. On the Build menu, click Build Solution.

  9. Run the MySolutionToolbarPkg project by pressing the keyboard shortcut, F5 or CTRL+F5.

    This launches the Visual Studio experimental build.

    NoteNote

    Both versions of Visual Studio are open at this time.

  10. In the Visual Studio experimental build, click Solution Explorer on the View menu.

    The toolbar should contain the new command button positioned to the right of the existing buttons. The button icon is the numeral 1 inside of a square.

  11. Click the new button

    You should see a dialog box with the message "Inside Company.MySolutionToolbarPkg.MySolutionToolbarPkg.MenuItemCallback()".

    NoteNote

    To add multiple buttons to the same group, add Command ID, Parent ID and Priority for each button in CMDPLACEMENT section. If you use the same Command ID for two or more buttons, these buttons are ignored by the build process as duplicates.

This section of the walkthrough demonstrates how to control the visibility of the button on the toolbar. By setting a context to single project or multiple projects in the VISIBILITY_SECTION section of MySolutionToolbarPkg.ctc file, the button is restricted to appearing only when either a single project or multiple projects are open in Visual Studio.

To display the button only when a single project or multiple projects are open

  1. If you have not already done so, close the Visual Studio experimental build.

  2. In Solution Explorer, expand the CtcComponents folder in the MySolutionToolbarPkg project to find the MySolutionToolbarPkg.ctc file. Right-click MySolutionToolbarPkg.ctc and select Open to open it in a text editor.

  3. In the BUTTONS_BEGIN section, replace the existing line defining the command with the following line. The DEFAULTINVISIBLE and DYNAMICVISIBILITY flags must be set in order for entries in the VISIBILITY section to take an effect.

    guidMySolutionToolbarPkgCmdSet:cmdidMyTestCmd,    // Command ID
    guidMySolutionToolbarPkgCmdSet:MyMenuGroup,       // Parent ID
    0x0100,                                           // Priority
    guidMySolutionToolbarPkgCmdSet:bmpPic1,           // Image
    BUTTON,                                           // Type
    DEFAULTINVISIBLE | DYNAMICVISIBILITY,             // Flags
    "My Button Test Command";                         // Command name
    
  4. In the VISIBILITY_SECTION section, add the following lines to set the context to a single and multiple projects.

    guidMySolutionToolbarPkgCmdSet:cmdidMyTestCmd,     // Command ID
    UICONTEXT_SolutionHasSingleProject;                // Single Project ID
    guidMySolutionToolbarPkgCmdSet:cmdidMyTestCmd,     // Command ID
    UICONTEXT_SolutionHasMultipleProjects;             // Multiple Projects ID
    
  5. Save the MySolutionToolbarPkg.ctc file and then close it.

  6. On the Build menu, click Build Solution.

  7. Run the MySolutionToolbarPkg project by pressing the keyboard shortcut, F5 or CTRL+F5. This launches the Visual Studio experimental build.

    NoteNote

    Both versions of Visual Studio are open at this time.

  8. Go to the View menu and open Solution Explorer in the Visual Studio experimental build. The toolbar does not contain the command button depicting a numeral 1.

  9. Go to the File menu and open any solution containing a project or multiple projects.

    The button to appears on the toolbar to the right of the existing buttons.

  10. Go to the File menu and select Close Solution. The button disappears from the toolbar.

    NoteNote

    The visibility of the button is controlled by Visual Studio until VSPackage is loaded. After the VSPackage is loaded, the visibility of the button is controlled by the VSPackage. The user provides the VSPackage implementation for controlling the visibility of the button.

Show: