Visual Studio Tip of the Day - Let's Build an Add-In

Yesterday the VS Ecosystem team released the Visual Studio 2008 SDK 1.0 and the Visual Studio 2008 shell. The VS 2008 SDK contains full native Visual Basic support and contains many VB samples. What was in the VB Pack for the VS 2005 SDK is now included in the VS 2008 SDK, plus more.

I've started playing with building Visual Studio Add-Ins in Visual Basic and it's really simple to get started. And what's really nice is you don't need to install the SDK to create Add-ins, they are built in. I've got the Tip of the Day Browser from Sara's rss feed running at startup now. Let's walk through how I did it in Visual Studio 2008. If you want to follow along here's the latest code for the Tip of the Day browser.

First I added a new project to the current Solution. If you don't see your solution in the Solution Explorer, go to Tools --> Options and select "Projects and Solutions" and check the box "Always show Solution". Now right-click on the solution and select Add --> New Project, then expand "Other Project Types" and select "Extensibility" then choose "Visual Studio Add-In".

This starts the Add-in wizard that helps you define some things about your add-in. The first step is to select your programming language so select "Create an Add-in using Visual Basic" and click next.

Next step is to select the application hosts. For this example we'll uncheck "Visual Studio 2008 Macros" since I just want this to appear at startup and on the Tools menu.

Next, type and name a description of your Add-in. For this example we'll set the name and description to "Visual Studio Tip of the Day".

Next, the Wizard asks us how we want to invoke the Add-in. For this example we just want to be able to load it at start up of Visual Studio and provide a menu item in the Tools menu. Select the appropriate check boxes and click Next.

  

The last thing the Wizard asks us is if we want an about box shown for our Add-in. For this example we'll skip this step so click Next and then Finish.

This process adds a new Add-in project to your solution. Next open the Add-in project properties by double-clicking on My Project and go to the Compile tab and select the Advanced Options button at the bottom of the page. Then select the .NET Framework 3.5 as the target Framework.

Next we'll need to add a reference to our Tip of the Day (SaraRssViewer) project. You can do this in the project properties --> References tab or you can right-click on the Add-in project and select Add Reference.

Now that we have the references and target framework set up, open the Connect.vb file and in the OnStartUpComplete method we will write code to load the form:

Public Sub OnStartupComplete(ByRef custom As Array) _

  Implements IDTExtensibility2.OnStartupComplete

   Dim frm As New SaraRSSViewer.Form1

   frm.Show()

End Sub

To run the form when the user selects our Add-in from the Tools menu, we'll need to put the same code to load the form into the Exec method at the bottom of the file:

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

    Dim frm As New SaraRSSViewer.Form1

    frm.Show()

    handled = True

    Exit Sub

    End If

    End If

End Sub

Last thing to do is set the Add-in project as the startup project so right-click on it and select "Set as Startup Project". Now hit F5 to compile and run. This will open up another instance of Visual Studio 2008 and you'll see the Tip of the Day window open:

If you go to the Tools menu you'll also see the menu item there as well. You can customize the name of the menu item by changing the line of code in the OnConnection method of the Connect class that defines the command variable. Change the third parameter to specify the text to display on the menu item and the 4th to change the tooltip.

Dim command As Command = _

    commands.AddNamedCommand2(_addInInstance, _

                              "MyAddin1", _

                              "Tip of the Day", _

                              "Display the Visual Studio tips of the day", _

                      True, 59, Nothing, _

                              CType(vsCommandStatus.vsCommandStatusSupported, Integer) + _

                              CType(vsCommandStatus.vsCommandStatusEnabled, Integer), _

                              vsCommandStyle.vsCommandStylePictAndText, _

                              vsCommandControlType.vsCommandControlTypeButton)

When we build the Add-in it places an .addin file in your Visual Studio 2008\Addins\ folder which tells VS which Add-ins to load. As you can see, it's pretty easy to get a simple Visual Studio Add-in up and running. I encourage you to take a look at the other samples included in the SDK.

In a future post we'll walk through how to deploy this Add-in to other machines.

Enjoy,
-B