Search Wiki:
This sample demonstrates how to create menu and command items and visualize them inside Visual Studio’s menus and toolbars.

Goals

  • 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

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 and to the debug output. (NOTE: there is a known issue with Visual Studio 2010 Beta 2 where the text will not appear on the Output Window of the experimental instance of Visual Studio. Please refer to the debug output instead.) 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:
  1. How to create simple menu and command items.
  2. How to place them inside other elements provided by other packages (for example, default Visual Studio menus or toolbars) or by this same package.
  3. How to modify the text or the visibility of a command at runtime.
  4. 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.


To start the sample:

  1. Open the MenuAndCommands.sln solution.
  2. Build the solution.
  3. Press F5 register it in the experimental instance and launch Visual Studio from the experimental instance.

To start the sample functionality:

  • On the View menu, click Output to display the Output window.
  • On the Tools menu, click C# Command Sample. A message appears in the <bold>Output</bold> window.
  • On the Tools menu, click C# Text Changes.
  • Click the Tools menu again and note that the text for that menu item has changed to indicate how many times you chose the command.
  • On the Tools menu, click 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.

Additional Resources


Unit Tests:


  • Create an instance.
  • Make sure the instance implements IVsPackage.
  • Make sure DynamicTestCommand returns the right text.

Last edited Nov 4 at 9:54 PM  by vsxcg, version 3
Updating...
Page view tracker