|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Walkthrough: Adding a Menu to the Visual Studio Menu Bar (C#)
This walkthrough shows how to add a menu to the menu bar of the Visual Studio integrated development environment (IDE). The IDE menu bar contains menu categories such as File, Edit, View, Window, and Help.
By completing this walkthrough, you create a menu named TestMenu that contains one command. If the command is unavailable, the menu does not appear.
For more information about menus and .vsct files, see Menus and Toolbars.
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.
To create the TopLevelMenu VSPackage
Create a VSPackage named TopLevelMenu. For more information, see How to: Create VSPackages (C# and Visual Basic).
In the Visual Studio Integration Package Wizard, set the programming language to Visual C#, select Menu Command, set the command name to Test Command, and set command ID to cmdidTestCommand.
To create a menu
In Solution Explorer, open TopLevelMenu.vsct.
Register a new menu. At the end of the file, there is a <Symbols> node that contains several <GuidSymbol> nodes. In the node named "guidTopLevelMenuCmdSet", create an <IDSymbol> node, as follows:
The name attribute is the ID of the menu and the value is a unique identifier so that collisions can be avoided.
Create an empty <Menus> node in the <Commands> node, just before <Groups>.
In the <Menus> node, create the following <Menu> node to define the menu you registered in step 2:
The guid and id values of the menu specify the command set and the specific menu in the command set.
The guid and id values of the parent place the menu on the section of the Visual Studio menu bar that contains the Tools and Add-ins menus.
The value of the <CommandName> string specifies that the text should appear in the menu item.
Change the GUID/ID pair of the parent of the generated <Group> node so that it is the same as that of the menu you created, as follows:
This makes the group part of the new menu.
Find the <Buttons> section. Notice that the Visual Studio Integration Package Wizard has generated a <Button> element that has its parent set to MyMenuGroup. As a result, this command will appear on your menu.
<Button guid="guidTopLevelMenuCmdSet" id="cmdidTestCommand" priority="0x0100" type="Button"> <Parent guid="guidTopLevelMenuCmdSet" id="MyMenuGroup" /> <Icon guid="guidImages" id="bmpPic1" /> <Strings> <CommandName>cmdidTestCommand</CommandName> <ButtonText>Test Command</ButtonText> </Strings> </Button>
To build and test the VSPackage
On the Build menu,click Build Solution.
Doing this rebuilds the .vsct file with the changes you made. Correct any errors that may occur during building. (The most common error is a GUID label or a command ID that has the wrong case; GUID labels and command IDs are always case-sensitive.)
Press F5 to open an instance of the Visual Studio experimental environment in debug mode.
Look at the menu bar in the Visual Studio experimental environment to find TestMenu positioned just before the Analyze menu.
Click TestMenu and then click Test Command.
A message box appears and displays the message "Inside Company.TopLevelMenu.TopLevelMenuPackage.MenuItemCallback()". This indicates that the new command works.