Creating Visual Studio Add-Ins
Visual Studio provides one of the most powerful IDEs on the market. One under-exploited aspect of this IDE is the extensibility model. Programming IDE’s are not static development tools. Development techniques, tools, and concepts change. Extensibility has been built into Visual Studio from its early inceptions. The great thing about Microsoft’s foresight is that you can augment functionality of Visual Studio yourself. This article will demonstrate how to extend the Visual Studio IDE using Visual Studio, the .NET Framework, and the Visual Studio add-in and automation models.
The Visual Studio IDE has a number of built in extensibility models.
- Macros/automation model
This article will mash the first three together to create a robust solution creation platform. You will learn how to create a basic add-in, how to extend that add-in using the automation model, and finally how to combine solution templates into the mix to make yourself much more productive. See Claudio Lassala’s article, “Creating and Distributing Packages with the Visual Studio SDK” in this CoDe Focus issue to learn more about how to extend Visual Studio 2008 using VSPackages.
The Add-In Defined
Developers choose to automate the Visual Studio IDE to increase their productivity. How many times have you found yourself performing the same tasks for your projects? You create a new project, make standard settings changes, you add the same folder structure, you add the same programs, add the same references, etc. As you develop more and more applications you may find yourself spending more time doing redundant project setup and configuration. Add-ins help you cut out redundancy and allow you to focus on core development tasks. You can take this mundane set of steps out of the process by using a combination of add-ins, macros, and templates.
Creating the Basic Add-In
As your first step you want to create the basic shell of your add-in. To create an add-in in Visual Studio, do the following:
- From the Visual Studio menu select File and choose New Project.
- In the New Project dialog box (Figure 1) in the left pane, select Other Project Types, expand the Extensibility type, and in the right pane choose Visual Studio Add-In and then click OK.
- Name your project. For this example I’ll use CodeFocusAddIn.
- The Add-In Wizard will then start up. On page 1 you need to specify the language you will use to develop your add-in. You can choose from Visual Basic, C#, J# and two styles of C++ development. In the add-in for this article, I’ll use Visual Basic.
- Page 2 allows you to specify the host(s) for your add-in. By default the hosts are Visual Studio 2005 and Visual Studio 2005 Macros. Choose the host(s) you wish to support.
- On page 3 you’ll provide the name and description of your add-in.
- Now you’ll specify some options for your add-in (Figure 2). On page 4, specify whether your add-in will be called from the toolbar, whether you want the add-in to load when the Visual Studio Shell starts, and whether the add-in will activate any modal forms. In the project for this article, I’ll choose to add an option to the toolbar.
- Page 5 allows you to provide information in the “About” box for your add-in.
- Finally on page 6 click Finish. The wizard will create your add-in project and all supporting files.
Figure 1: Visual Studio Shell options to create a new add-in.
Figure 2: New add-in dialog box with options specified to add a toolbar item and auto load the add-in into the host application.
At this point you want to look at two specific files: <your-project-name>.AddIn and Connect.VB. The .AddIn file contains XML information that is used to register your add-in with the Visual Studio shell. Listing 1 contains the contents of this XML.
The Connect.VB file contains the guts of your add-in. This code implements two interfaces: IDTExtensibility2 and IDTCommandTarget, which the Visual Studio IDE uses to communicate with your add-in. To add custom code to your add-in you’ll use the Connect.VB. file.
By: Rod Paddock
Rod Paddock is the editor of CoDe Magazine. Rod has been a software developer for more than 10 years and has worked with tools like Visual Studio .NET SQL Server, Visual Basic, Visual FoxPro, Delphi and numerous others.
Rod is president of Dash Point Software, Inc. Dash Point is an award winning software development firm that specializes in developing applications for small to large businesses. Dash Point has delivered applications for numerous corporations like: Six Flags, First Premier Bank, Intel, Microsoft and the US Coast Guard.
Rod is also VP of Development for SQL Server tools maker, Red Matrix Technologies. (www.redmatrix.com).
Visual Studio 2008 add-ins, combined with the Visual Studio object model and templates, create a useful way to customize the Visual Studio 2008 IDE.