An add-in is a tool that you create programmatically by using objects, methods, properties, collections in .NET's extensibility model. This compiled application enables you to automate the difficult and tedious tasks within the visual studio integrated development environment (IDE). These tasks can be accomplished in response to an event, such as the mouse being clicked, forms being added to the project or control being added to the form. The actions may not be visible to the developer. An add-in extends the functionality of the Visual Studio IDE. Extensibility is the mechanism exposed to the developer that provides the ability to enhance and extend the functionality of the IDE. It basically exposed IDE's internal functions to add-in developer.
Add-in's can be invoked in many ways
- Through Add-in Manager
- Toolbars command or buttons
- Development environment (devenv) Command Line
- Events such as IDE start-up
Powers of Add-in:
- You have access to all the features of .NET to modify IDE.
- You can write add-in's using any of the Visual Studio language
- Add-in's are compiled, so code is not exposed
- Add-in's are easy to distribute as they are compiled
Extensibility Object Model for Visual Basic .Net and Visual C#.Net:
Visual Studio .NET provides a general extensibility model for all languages sharing the IDE. Additionally it provides a specific object model that pertains only to visual Basic .NET and Visual C#.NET. The VsLangProj namespace provides the ability to manipulate project attributes, which are found only in Visual Basic.NET and Visual C#.NET.
DTE stands for Development Tools Extensibility. The DTE object is the root object. It is literally a pointer to the IDE through which all other objects that are exposed by IDE are referenced. An instance of the DTE object is passed to the OnConnection method that you implement when you create an add-in.
Creating an add-in using Add-in Wizard:
Creating an add-in using Add-in Wizard is much easier and faster. Add-in wizard creates add-in project and set-up project. Wizardcreates .reg file to register add-in. It also creates GUID for add-in and ensures all required methods are included in Connect class. We will see one simple add-in which shows alert box when it is run. To create add-in project, you have to do the following steps
Setting the Project Type:
Open new project and select Other Projects -> Extensibility Projects in Project Types and select Visual-Studio .Net add-in in templates. Enter Name of the Add-in Project and Select Location where you want you add-in to be placed using Browse Button. After clicking ok, you will get Add-in Welcome wizard. Click Next
Setting the Add-in's Base Language
Clicking Next will open up the Step1 of the add-in wizard. Here select the Visual C# language option. Click Next.
Selecting the Application Host
Step-2 of the wizard contains the list of all application host. Choose Visual studio .NET .Click Next. This will allow the add-in to be used only in the Visual Studio .NET IDE. Click next to proceed Step 3 of the wizard.
Entering a name and description
This step allows to enter a friendly name and description for the new add-in. This name appears in the Add-in Manager's dialog box Available Add-ins list and gives the user short description of what the add-in does. Enter "OpenAlertBox" in the Name and in Description enter "This is my first Add-in wizard". Click Next to proceed Step 4 of the wizard.
Choosing the Add-in Options
Step 4 presents you with several options for customizing the add-in options. Choose the option "Would you like to create an UI for the user to interact with your add-in?" .This option causes the wizard to create a toolbar button and accompanying menu items automatically. Choose the option "I would like to my add-in to load when the host application starts" in "You can fine tune when your add-in loads". Choose the option "Setting Up Privileges Permission" .This option specifies who can use the add-in.
Creating an About Box
You can check the option if you want to display the about box. Click Next you will see the summary of options you have selected.
Exploring the Connect class methods:
We will explore some of the methods and events of Connect class..
OnConnection method is the method that is first called by the IDE when it starts the add-in. Three parameters are passed to the method. First is the pointer to the IDE .Second is the ConnectMode. This parameter has the following values
- ext_cm_AfterStartup - 0: The add-in was loaded after the application started or by setting the Connect property of the corresponding add-in to True.
- ext_cm_Startup - 1: The add-in was loaded at IDE start-up.
- ext_cm_External - 2: The add-in was loaded externally by another program or component.
- ext_cm_CommandLine - 3: The add-in was loaded through the Visual Studio devenv command line.
- ext_cm_Solution - 4: The add-in was loaded when a user loaded a solution that required the add-in.
- ext_cm_UISetup - 5: The add-in was started for the first time since being installed
The third parameter, addInInst, is an object representing the instance of the add-in. It is passed to the AddNamedCommand method of the Commands object when adding a menu to the IDE.The OnConnection method is the obvious point to place your UI (menus,toolbars, tool buttons) through which the user will communicate to the add-in. This method is also the place to put your validation code if you are licensing the add-in. You would normally do this before putting up the UI. If the user is not a valid user, you would not want to put the UI into the IDE.
This event occurs when the add-in is unloaded.
When the user clicks a command (menu or tool button), the QueryStatus event is fired. The QueryStatus event returns the current status of the specified named command, whether it is enabled, disabled, or hidden in the vsCommandStatus parameter, which is passed to the event by reference. The event has the following parameters:
- CmdName: The name of the command to check.
- NeededText: A vsCommandStatusTextWanted constant specifying whether
information is returned from the check, and if so, what type of
information is returned.
- StatusOption: A vsCommandStatus specifying the current status of the
command. This parameter determines if the Exec event will be fired.
- CommandText: The text to return if vsCommandStatusTextWantedStatus is specified.
The Exec event is fired after the QueryStatus event is fired, assuming that the return to the statusOption parameter of QueryStatus is supported and enabled. This is the event where you place the actual code for handling the response to the user click on the command.
Open Connect.vb file. Make the changes shown below...
Move Commandobj to Module level:
Public Class Connect
Dim applicationObject As EnvDTE.DTE
Dim addInInstance As EnvDTE.AddIn
' **** moved to module level so OnDisconnect can see it *****
Dim CommandObj As Command
Changes to the OnConnection method:
Public Sub OnConnection(ByVal application As Object, _
ByVal connectMode As ext_ConnectMode, _
ByVal addInInst As Object, _
ByRef custom() As Object) _
applicationObject = CType(application, EnvDTE.DTE)
addInInstance = CType(addInInst, EnvDTE.AddIn)
' changed test of connectmode from
If connectMode = ext_ConnectMode.ext_cm_Startup Then
Dim objAddIn As AddIn = CType(addInInst, AddIn)
' moved to module level so OnDisconnect can see it
' Dim CommandObj As Command
CommandObj = _
"Click Me", _
"Executes the command for MyAddinTest1", _
True, 59, Nothing, 1 + 2)
Catch e As System.Exception
MsgBox("Can't place toolbutton, error: " & _
e.Message, MsgBoxStyle.Critical, _
Changes to the OnDisconnection Method:
Public Sub OnDisconnection( _
ByVal RemoveMode As ext_DisconnectMode, _
ByRef custom() As Object) _
' display a message to tell what we are about to do
MsgBox("Disconnect, remove Tool Button", _
' remove the add-in command button
Catch e As System.Exception
' if we should fail to remove the button, display the error
MsgBox("Error in Disconnect: " & _
Changes to the OnExec method:
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 = "TestAddin.Connect.TestAddin" Then
handled = True
Installing the Add-in:
Build the add-in application. If it is successful ,right click on the SetUp Project and click install.Set-up wizard will appear.Click Next to proceed the steps.You have add-in installed on your machine.
Viewing the added Add-in:
Open the Visual Studio.Net and Check in Tools Options. Newly installed add-in will appear. Click on it Message Box with message "Test Addin" will appear. You have successfully created add-in for Visual studio.Net.