Implementing Named Commands
Use the IDTCommandTargetinterface only if you intend to provide one or more custom named commands. A named command with Visual Studio .NET is merely a piece of functional code that is assigned a well-known name within the context of the entire collection of commands (or macros) programmatically accessible, or also invoked directly through the IDE's Command window. Almost every action a developer can initiate within the IDE is made available in the form of a named command. To see this in action, open the Command window and type View.FullScreen
. This will change your IDE configuration to the Full Screen mode.
The benefit of exposing add-in functionality as a named command will be two-fold. As a convenience, or for programmatic accessibility of your add-in's functionality, a named command is the standard mechanism the developer will use. The second benefit will stem from the ease of adding menu items to context menus or command bars through use of the AddControl
method of the EnvDTE.Command
Dim oAI As AddIn = CType(addInInst, AddIn)
Dim oCmd As Command
oCmd = IDE.Commands.AddNamedCommand( _
oAI, "CmdName", "ButtonText", _
"Tooltip", True, 59, Nothing, _
1 + 2)
Catch e As System.Exception
The IDTCommandTargetinterface exposes two method members, Exec
, which you will implement. You will need to implement both so that you can respond properly to the IDE when Visual Studio .NET invokes your named commands. The QueryStatus
method implementation provided by your add-in will be called in three scenarios:
- Your named command is manually entered into the Command window
- The ExecuteCommand method of the core EnvDTE.DTE object is used to request your named command
- The IDE needs to draw a menu item associated with your named command through the original call to AddControl.
You should assign a value from the vsCommandStatus
enumeration to the statusOption
parameter being passed by reference. Your value should indicate the command is both available (vsCommandStatusEnabled
) and supported (vsCommandStatusSupported
). This will instruct the IDE to invoke the Exec
method, providing your add-in the chance to handle the command request.
StatusOption = CType( _
vsCommandStatus.vsCommandStatusEnabled + _
You should place the necessary named command implementation code within the Exec
method. You need a logic check to guarantee that the proper code is executed for the command being requested.
If cmdName = "FromScratch.MyAddIn.CmdName" Then
handled = True
The actual name of your command will be a combination of the ProgID set with the ProgIdAttribute
class applied to your add-in class and the second parameter passed into the AddNamedCommand