How to: Expose an Add-In as a Button on the Toolbar
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.
If you choose the option to create a user interface (UI) when using the Add-In Wizard to create your add-in, then the wizard creates a command for the add-in on the Tools menu. If you want to display your add-in in a more prominent or easily accessible location — such as on the main Visual Studio toolbar, also known as the "standard" toolbar — then you can do this as well.
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.
Procedure
To display an add-in on the standard toolbar
Create or open an add-in project.
Replace the add-in's code with the code below.
Example
The following example demonstrates how to create an add-in that adds a button on the Visual Studio "standard" toolbar. (That is the name of the toolbar in Visual Studio.)
You use the AddNamedCommand2 method to create a command for the add-in.
You then obtain a reference to the standard toolbar.
Finally, you use the AddControl method to add a new button.
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Public Class Connect
Implements IDTExtensibility2
Implements IDTCommandTarget
Dim _applicationObject As DTE2
Dim _addInInstance As AddIn
Dim stdCmdBarCtl As CommandBarControl
Public Sub New()
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
Dim cmd As Command
Dim stdCmdBar As CommandBar
Dim cmdBarBtn As CommandBarButton
Try
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
Select Case connectMode
Case ext_ConnectMode.ext_cm_AfterStartup, _
ext_ConnectMode.ext_cm_Startup
' Add the command
cmd = _applicationObject.Commands. _
AddNamedCommand(_addInInstance, _
"ANewCommand", "ANewCommand", _
"A new command", True, 59, Nothing, _
vsCommandStatus.vsCommandStatusSupported _
Or vsCommandStatus.vsCommandStatusEnabled)
' Reference the Visual Studio standard toolbar.
stdCmdBar =
CType(_applicationObject.CommandBars.Item _
("Standard"), _
Microsoft.VisualStudio.CommandBars.CommandBar)
' Add a button to the standard toolbar.
stdCmdBarCtl = CType(cmd.AddControl(stdCmdBar, _
stdCmdBar.Controls.Count + 1), _
Microsoft.VisualStudio.CommandBars. _
CommandBarControl)
' Set a caption for the toolbar button.
stdCmdBarCtl.Caption = "A new command bar"
' Set the toolbar's button style to an icon button.
cmdBarBtn = CType(stdCmdBarCtl, CommandBarButton)
cmdBarBtn.Style = MsoButtonStyle.msoButtonIcon
End Select
Catch e As System.Exception
System.Windows.Forms.MessageBox.Show(e.ToString)
End Try
End Sub
Public Sub OnDisconnection(ByVal disconnectMode As _
ext_DisconnectMode, ByRef custom As Array)
' Implements IDTExtensibility2.OnDisconnection()
Try
' When the add-in closes, get rid of the toolbar button.
If Not (stdCmdBarCtl Is Nothing) Then
stdCmdBarCtl.Delete()
End If
Catch e As System.Exception
System.Windows.Forms.MessageBox.Show(e.ToString)
End Try
End Sub
Public Sub OnAddInsUpdate(ByRef custom As Array) Implements _
IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete(ByRef custom As Array) Implements _
IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnBeginShutdown(ByRef custom As Array) Implements _
IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub QueryStatus(ByVal commandName As String, ByVal _
neededText As vsCommandStatusTextWanted, ByRef status As _
vsCommandStatus, ByRef commandText As Object) Implements _
IDTCommandTarget.QueryStatus
If neededText = EnvDTE.vsCommandStatusTextWanted. _
vsCommandStatusTextWantedNone Then
If commandName = "cmdBar2.Connect.ANewCommand" Then
status = CType(vsCommandStatus.vsCommandStatusEnabled _
+ vsCommandStatus.vsCommandStatusSupported, _
vsCommandStatus)
Else
status = vsCommandStatus.vsCommandStatusUnsupported
End If
End If
End Sub
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 = "cmdBar2.Connect.ANewCommand" Then
handled = True
System.Windows.Forms.MessageBox.Show("Add-in running")
Exit Sub
End If
End If
End Sub
End Class
See Also
Tasks
How to: Control Add-Ins By Using the Add-In Manager
Reference
PAVE Visual Studio Commands and Switches
Concepts
Displaying Add-Ins on Toolbars and Menus