Menu and Commands Sample
Create menu and command items and visualize them inside Visual Studio’s menus and toolbars.
- Technologies: Visual Studio 2017 SDK
- Topics: Visual Studio Shell, VSX, Menu
This Visual Studio Package demonstrates how to create menu and command items and visualize them inside Visual Studio’s menus and toolbars.
- Adding a menu item / command to Visual Studio and handling it
- Placing commands in various places (Solution Explorer toolbar, custom toolbar, Tools menu, editor context menu)
- Dynamic text in menu items
- Associating a keybinding (keyboard shortcut) to a menu item
Get all samples
Clone the repo (How to):
git clone https://github.com/Microsoft/VSSDK-Extensibility-Samples.git
Run the sample
- To run the sample, hit F5 or choose the Debug > Start Debugging menu command. A new instance of Visual Studio will launch under the experimental hive.
- Once loaded, select the View > Output menu command to display the Output window.
- Select Tools > C# Command Sample. A message appears in the Output window.
- Select Tools > C# Text Changes.
- Open the Tools menu again and note that the text for that menu item has changed to indicate how many times you chose the command.
- Select Tools > C# Dynamic Visibility 1.
- Click Tools menu again and note that the menu item has disappeared and been replaced by a C# Dynamic Visibility 2 command. Click it and C# Dynamic Visibility 1 returns.
Source Code Overview
The main focus of this sample is the VSCT file containing the definition of these elements. The code is minimal. The event handler functions that are called when the user executes the commands simply write a message on the Output window. The only exceptions are the callback for the menu items with dynamic properties (text or visibility). In this case, the properties will be changed according to some logic.
This sample is organized into four main areas:
- How to create simple menu and command items.
- How to place them inside other elements provided by other packages (for example, default Visual Studio menus or toolbars) or by this same package.
- How to modify the text or the visibility of a command at runtime.
- How to associate a keyboard accelerator to a command.
Inside the VSCT file, the command definition section defines a new toolbar, some menu groups, and a few commands. This section contains all the interesting parts about areas 1 and 3. The button subsection of the command definition section includes the usage of different visibility flags. These flags allow us to tell Visual Studio that we want to programmatically change a specific set of command properties when our package is loaded.
The second section in the VSCT file, the command placement section, is of interest for area 2 listed above. In this section, you can see how to place a command inside a menu group or a menu group inside a menu.
The last section, the key binding section, allows associations between commands and keyboard accelerators.