Walkthrough: Adding a Command to the Solution Explorer Toolbar (C#)
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 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
-
Open Visual Studio. On the File menu, point to New and select Project.
-
In the New Project dialog box, select Other Project Types and then select Extensibility from the Project types list.
-
In the Templates list, select Visual Studio Integration Package.
-
Type in the name of your project; for example, MySolutionToolbarPkg. The rest of this walkthrough assumes that name.
-
Click OK to open the Visual Studio Integration Package Wizard.
-
Click the Next button on the Welcome to the Visual Studio Integration Package Wizard page.
-
In the Select a Programming Language page, select Visual C# and accept the default for the key file, and then click Next.
-
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.
-
In the Select VSPackage Options page, select Menu Command and then click Next.
-
In the Command Options page, set the Command Name to My Button Test Command and set the Command ID as cmdidMyTestCmd. Click Finish.
-
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
-
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.
-
In the Menu Group IDs section, add a definition for MySolutionToolbarGroup:
#define MySolutionToolbarGroup 0x1090
-
Click the Save button and then close the CommandIds.h file.
-
Also in the CtcComponents folder in Solution Explorer, right click MySolutionToolbarPkg.ctc and select Open to open it in a text editor.
-
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
-
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
-
Click the Save button and then close the MySolutionToolbarPkg.ctc file.
-
On the Build menu, click Build Solution.
-
Run the MySolutionToolbarPkg project by pressing the keyboard shortcut, F5 or CTRL+F5.
This launches the Visual Studio experimental build.
Note Both versions of Visual Studio are open at this time.
-
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.
-
Click the new button
You should see a dialog box with the message "Inside Company.MySolutionToolbarPkg.MySolutionToolbarPkg.MenuItemCallback()".
Note 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
-
If you have not already done so, close the Visual Studio experimental build.
-
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.
-
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
-
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
-
Save the MySolutionToolbarPkg.ctc file and then close it.
-
On the Build menu, click Build Solution.
-
Run the MySolutionToolbarPkg project by pressing the keyboard shortcut, F5 or CTRL+F5. This launches the Visual Studio experimental build.
Note Both versions of Visual Studio are open at this time.
-
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.
-
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.
-
Go to the File menu and select Close Solution. The button disappears from the toolbar.
Note 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.