How to: Add and Handle Commands
Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see FAQ: Converting Add-ins to VSPackage Extensions.
The following objects allow you to create, handle, and manipulate commands on Visual Studio menus and toolbars.
Object Name |
Description |
---|---|
Provides methods to determine the status of, or execute, a command added to the integrated development environment (IDE) by using the AddNamedCommand2 method. |
|
Represents all commands in the IDE. |
|
Represents a command in the IDE. |
|
Provides command events for add-ins. |
|
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 recreate the command, close all instances of Visual Studio and double-click the file named ReCreateCommands.reg in the folder containing the source code for your Add-in.
Using these objects, you can:
Add or remove a command bar in the Visual Studio IDE (AddCommandBar and RemoveCommandBar methods).
Add a new named command to a toolbar or menu (AddNamedCommand2 method).
Obtain the status of a command (CommandInfo and QueryStatus methods).
Note
You cannot connect a CommandBarEvents event for CommandBar controls that were created for a new command added through AddNamedCommand2.
Note
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and ExportSettings on the Tools menu. For more information, see Customizing Development Settings in Visual Studio.
Example
The following example uses:
The Command object.
The AddNamedCommand method.
The AddControl method.
The IDTCommandTarget interface.
The IDTCommandTarget.Exec method.
The IDTCommandTarget.QueryStatus method.
The procedure demonstrates how to make an add-in appear as a command on the Tools menu in Visual Studio. Add the first section of code to the OnConnection method of the add-in you create. In the Exec and QueryStatus methods, make sure that the line, If cmdName = "MyAddin1.Connect.MyAddin1" Then, reflects the name of your add-in.
Public Sub OnConnection(ByVal application As Object, _
ByVal connectMode As ext_ConnectMode, ByVal addInInst _
As Object, ByRef custom As Array) Implements _
IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
If connectMode = ext_ConnectMode.ext_cm_UISetup Then
Dim commands As Commands2 = CType(_applicationObject. _
Commands, Commands2)
Dim toolsMenuName As String
Try
Dim resourceManager As System.Resources. _
ResourceManager = New System.Resources. _
ResourceManager("MyAddin1.CommandBar", _
System.Reflection.Assembly.GetExecutingAssembly())
Dim cultureInfo As System.Globalization. _
CultureInfo = New System.Globalization. _
CultureInfo(_applicationObject.LocaleID)
toolsMenuName = resourceManager.GetString _
(String.Concat(cultureInfo. _
TwoLetterISOLanguageName, "Tools"))
Catch e As Exception
toolsMenuName = "Tools"
End Try
Dim commandBars As CommandBars = CType(_applicationObject _
.CommandBars, CommandBars)
Dim menuBarCommandBar As CommandBar = commandBars. _
Item("MenuBar")
Dim toolsControl As CommandBarControl = _
menuBarCommandBar.Controls.Item(toolsMenuName)
Dim toolsPopup As CommandBarPopup = CType(toolsControl, _
CommandBarPopup)
Try
Dim command As Command = commands.AddNamedCommand2 _
(_addInInstance, "MyAddin1", "MyAddin1", _
"Executes the command for MyAddin1", True, 59, _
Nothing, CType(vsCommandStatus. _
vsCommandStatusSupported, Integer) + CType _
(vsCommandStatus.vsCommandStatusEnabled, Integer), _
vsCommandStyle.vsCommandStylePictAndText, _
vsCommandControlType.vsCommandControlTypeButton)
command.AddControl(toolsPopup.CommandBar, 1)
Catch argumentException As System.ArgumentException
MsgBox(argumentException.ToString)
End Try
End If
End Sub
'Code for the QueryStatus method.
Public Sub QueryStatus(ByVal commandName As String, _
ByVal neededText As vsCommandStatusTextWanted, _
ByRef status As vsCommandStatus, ByRef commandText _
As Object) Implements IDTCommandTarget.QueryStatus
If neededText = vsCommandStatusTextWanted. _
vsCommandStatusTextWantedNone Then
If commandName = "MyAddin1.Connect.MyAddin1" Then
status = CType(vsCommandStatus. _
vsCommandStatusEnabled + vsCommandStatus. _
vsCommandStatusSupported, vsCommandStatus)
Else
status = vsCommandStatus.vsCommandStatusUnsupported
End If
End If
End Sub
' Code for the Exec method.
Public Sub Exec(ByVal commandName 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 commandName = "MyAddin1.Connect.MyAddin1" Then
handled = True
Exit Sub
End If
End If
End Sub
public class Connect : Object, IDTExtensibility2, IDTCommandTarget
{
public Connect()
{
}
public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;
string toolsMenuName;
try
{
ResourceManager resourceManager = new
ResourceManager("MyAddin4.CommandBar",
Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new
System.Globalization.CultureInfo
(_applicationObject.LocaleID);
string resourceName =
String.Concat(cultureInfo.TwoLetterISOLanguageName,
"Tools");
toolsMenuName = resourceManager.GetString(resourceName);
}
catch
{
toolsMenuName = "Tools";
}
CommandBar menuBarCommandBar =
((CommandBars)_applicationObject.CommandBars)["MenuBar"];
CommandBarControl toolsControl =
menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
try
{
Command command = commands.AddNamedCommand2(_addInInstance,
"MyAddin4", "MyAddin4", "Executes the command for
MyAddin4", true, 59, ref contextGUIDS,
(int)vsCommandStatus.vsCommandStatusSupported+
(int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStylePictAndText,
vsCommandControlType.vsCommandControlTypeButton);
if((command != null) && (toolsPopup != null))
{
command.AddControl(toolsPopup.CommandBar, 1);
}
}
catch(System.ArgumentException)
{
}
}
}
public void QueryStatus(string commandName, vsCommandStatusTextWanted
neededText, ref vsCommandStatus status, ref object commandText)
{
if(neededText ==
vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName == "MyAddin4.Connect.MyAddin4")
{
status = (vsCommandStatus)vsCommandStatus.
vsCommandStatusSupported|vsCommandStatus.
vsCommandStatusEnabled;
return;
}
}
}
public void Exec(string commandName, vsCommandExecOption executeOption,
ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption ==
vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin4.Connect.MyAddin4")
{
handled = true;
return;
}
}
}
See Also
Tasks
Walkthrough: Creating a Wizard
Concepts
Controlling Projects and Solutions