Share via


Adding and Handling Commands

The following objects allow you to create, handle, and manipulate commands on Visual Studio .NET menus and toolbars.

Object Name Description
IDTCommandTarget interface Provides methods to determine the status of, or execute, a command added to the IDE using the AddNamedCommand method.
Commands collection Represents all of the commands in the integrated development environment (IDE).
Command object Represents a command in the IDE.
CommandEvents object Provides command events for Add-ins.
CommandBarEvents object Provides a Click event for when a control on a command bar is clicked.

Note   If your command no longer appears on the appropriate command bar, or if you add a new command or modify an existing command, or if you would like to re-create the command, close all instances of Visual Studio .NET and double-click the file "ReCreateCommands.reg" in the folder containing the source code for your Add-in.

Using these objects, you can:

Note   You cannot connect a CommandBarEvent event for CommandBar controls that were created for a new command added through Commands.AddNamedCommand.

Add Named Command Example

The following example uses both the Command object and its AddNamedCommand and AddControl methods, and the IDTCommandTarget interface and its two methods (Exec and QueryStatus) to demonstrate how to make an Add-in appear as a command on the Tools menu in Visual Studio .NET.

Implements IDTCommandTarget

Dim applicationObject As EnvDTE.DTE
Dim addInInstance as EnvDTE.AddIn
   
Dim objAddIn As AddIn = CType(addInInst, AddIn)
Dim CommandObj As Command
Try
   CommandObj = applicationObject.Commands.AddNamedCommand(objAddIn, "MyAddin1", "MyAddin1", "Executes the command for MyAddin1", True, 59, Nothing, 1 + 2)
   ' 1 + 2 == vsCommandStatusSupported + vsCommandStatusEnabled.
   CommandObj.AddControl(applicationObject.CommandBars.Item("Tools"))
Catch e as System.Exception
End Try

Public Sub Exec(ByVal cmdName As String, ByVal executeOption As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) Implements IDTCommandTarget.Exec
   handled = False
   If (executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault) Then
      If cmdName = "MyAddin1.Connect.MyAddin1" Then
         handled = True
         Exit Sub
      End If
   End If
End Sub
   
Public Sub QueryStatus(ByVal cmdName As String, ByVal neededText As vsCommandStatusTextWanted, ByRef statusOption As vsCommandStatus, ByRef commandText As Object) Implements IDTCommandTarget.QueryStatus
   If neededText = EnvDTE.vsCommandStatusTextWanted. vsCommandStatusTextWantedNone Then
      If cmdName = "MyAddin1.Connect.MyAddin1" Then
         statusOption = CType(vsCommandStatus.vsCommandStatusEnabled & vsCommandStatus.vsCommandStatusSupported, vsCommandStatus)
      Else
         statusOption = vsCommandStatus.vsCommandStatusUnsupported
      End If
   End If
End Sub  

See Also

Controlling Projects and Solutions | Creating and Controlling Environment Windows | Creating Add-Ins and Wizards | Creating an Add-In | Creating a Wizard | Automation and Extensibility Reference | Automation Object Model Chart