Writing Your First Add-in

Now that you have a good understanding of what steps make up the code generated by the Add-in Project Wizard, you're going to write some code that interacts with the Toolbox. To get started, follow these steps:

1.
Add a new Windows Form to your FirstAddin project and name it ToolboxTester. Change the Text property of the form to Toolbox Tester, and change the FormBorderStyle property to FixedToolWindow.

2.
Add a Button to the form. Change the Name property to ShowToolBoxTabs and change the Text property to "Show Toolbox Tabs".

3.
Add a ListBox control to the form and change its Name property to lb.

4.
Add a Button to the form and change its Name property to AddToolboxItems. Change its Text property to "Add Toolbox Items".

Your completed form should look like Figure 17.11.

Figure 17.11. The ToolboxTester form after adding new controls.


The goal of this add-in is to work with the Toolbox via a custom form that your FirstAddin project loads when Visual Studio .NET starts.

The first step is to notify the Exec method in the Connect.vb or Connect.cs class file that you want to load the new form when the add-in executes. To do so, your Exec method should look like Listing 17.5.

Listing 17.5. The Exec Method Code in the Connect Class File
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 = "firstAddin_vb.Connect.firstAddin_vb" Then

         handled = True

         Dim f As New ToolboxTester(applicationObject)
         f.Show()

         Exit Sub
      End If
   End If
End Sub


public void Exec(string commandName,
   EnvDTE.vsCommandExecOption executeOption,
   ref object varIn, ref object varOut, ref bool handled)
{
   handled = false;
   if(executeOption ==
      EnvDTE.vsCommandExecOption
      .vsCommandExecOptionDoDefault)
   {
      if(commandName ==
         "firstAddin_cs.Connect.firstAddin_cs")
      {
         handled = true;

         ToolboxTester f = new ToolboxTester(applicationObject);
         f.Show();

         return;
      }
   }
}

The code in Listing 17.5 is straightforward. You're simply showing a form. The difference is in the applicationObject that you're passing to the ToolboxTester form. Because you must tell the form that it's dealing with a specific instance of the IDE, you must pass the applicationObject variable to the form that represents this instance of the development tools extensibility (DTE) object. Right now, the project won't build because the New constructor in the form isn't expecting a variable to be passed to it.

The next step is to modify the ToolboxTester class file to implement the add-in code. Double-click the ToolboxTester form to get to the code-behind and add the using or Imports statement from Listing 17.6 to the top of your class file.

Listing 17.6. Adding the Correct Namespace Aliases to the ToolboxTester Class File
Imports System.Text
Imports EnvDTE
Imports System.Runtime.InteropServices


using System.Text;
using EnvDTE;
using System.Runtime.InteropServices;

Adding these namespaces to the class gives you access to the namespaces you need in the code. The most important namespace is the EnvDTE namespace, which gives you access to all the automation objects, such as the Toolbox.

Next you must add a class-level variable to hold the applicationObject's DTE from the Connect class file. Listing 17.7 shows you the DTE variable declaration and where you should put it in the class file.

Listing 17.7. Creating a Class-Level applicationObject Variable to Hold This Instance of the IDE Handle
Public Class ToolboxTester
   Inherits System.Windows.Forms.Form

   Private applicationObject As EnvDTE.DTE


public class ToolboxTester : System.Windows.Forms.Form
{
   private _DTE applicationObject;

Next you must modify the constructor of the form that will accept the applicationObject passed from the Connect class file, and then set the local applicationObject variable. Listing 17.8 is a modified constructor for the ToolboxTester class file.

Listing 17.8. Modifying the Constructor for the ToolboxTester Class to Accept the IDE Instance
Public Sub New(ByVal thisDTE As EnvDTE.DTE)

   MyBase.New()

   InitializeComponent()

   applicationObject = thisDTE

End Sub


public ToolboxTester(_DTE thisDTE)
{

   InitializeComponent();

   applicationObject = thisDTE;
}

Now that the DTE extensibility object is correctly passed from the Connect class file to the ToolboxTester, the application builds with no errors. But no code actually does anything yet, so you must add code to the click events for the two buttons you added to the form.

But before doing this, you must look at the Toolbox object that you're using. In the hierarchy of the DTE object, the second level is the Windows collection. Within the Windows collection are the individual collections that make up the objects in the IDE, including the Toolbox. Table 17.3 lists the Toolbox objects that you have access to.

Table 17.3. Toolbox Objects and Their Descriptions
ObjectDescription
ToolboxRepresents the Toolbox
ToolboxTabsRepresents all the tabs in the Toolbox
ToolboxTabRepresents a tab in the Toolbox
ToolboxItemsReturns a collections of all the items in a tab in the Toolbox
ToolboxItemRepresents a single item in a ToolboxItems collection

To see how this works, the following code snippet creates a win variable that is the current Toolbox in the DTE.Windows collection. After the object variable is created, you can create Toolbox and ToolboxTabs variables, which give you access to the properties, methods, and events of the Toolbox objects.

' Create an object reference to the IDE's
' ToolBox object.

Dim win As Window = DTE.Windows.Item _
(Constants.vsWindowKindToolbox)

Dim tb As ToolBox = win.Object
Dim tbTabs As ToolBoxTabs

' Create an object reference to the ToolBoxTabs object.
tbTabs = tb.ToolBoxTabs

' List the total number of tabs in the ToolBox.

 MessageBox.Show("Number of ToolBox tabs: " & tbTabs.Count)

To display the ToolboxTabs in the list box and then add a new ToolboxTab to the Toolbox, add the code in Listing 17.9 to the ShowToolboxTabs_click event and the AddToolboxTab_click event.

Listing 17.9. Adding the Toolbox Manipulation Code in the ShowToolboxTabs_click Event
Private Sub showToolboxTabs_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles showToolboxTabs.Click

      Dim tb As ToolBox
      tb = applicationObject.Windows.Item _
       (EnvDTE.Constants.vsWindowKindToolbox).Object

      Dim tbi As ToolBoxTab

      For Each tbi In tb.ToolBoxTabs
            lb.Items.Add(tbi.Name)
      Next

End Sub

Private Sub addToolboxTab_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles addToolboxTab.Click

      Dim tb As ToolBox
      Dim tbTab As ToolBoxTab

      tb = applicationObject.Windows.Item _
       (Constants.vsWindowKindToolbox).Object

      tbTab = tb.ToolBoxTabs.Add("New Toolbox Tab")

      tbTab.ToolBoxItems.Add("Code Snippet", "Dim x as integer", _
       vsToolBoxItemFormat.vsToolBoxItemFormatText)

      tbTab.ToolBoxItems.Add("HTML Snippet", "<h1>Hello World</h1>", _
      vsToolBoxItemFormat.vsToolBoxItemFormatHTML)

End Sub


private void showToolboxTabs_Click(object sender,
   System.EventArgs e)
   {

      ToolBox tb;
      tb = (ToolBox)applicationObject.Windows.Item
        (EnvDTE.Constants.vsWindowKindToolbox).Object;

      foreach (ToolBoxTab tbi in tb.ToolBoxTabs)
      {
        lb.Items.Add(tbi.Name.ToString());
      }
         }

private void AddToolboxItems_Click(object sender,
   System.EventArgs e)
    {

      ToolBox tb;
      tb = (ToolBox)applicationObject.Windows.Item
      (EnvDTE.Constants.vsWindowKindToolbox).Object;

         ToolBoxTab tbTab;
         tbTab = tb.ToolBoxTabs.Add("New Toolbox Tab");

         tbTab.ToolBoxItems.Add("Code Snippet", "Dim x as integer",
         vsToolBoxItemFormat.vsToolBoxItemFormatText);

         tbTab.ToolBoxItems.Add("HTML Snippet", "<h1>Hello World</h1>",
         vsToolBoxItemFormat.vsToolBoxItemFormatHTML);
     }

Now that you've written the necessary code for the Toolbox to interact with in your add-in, you need to build the project. The best way to make sure that your add-in works is to install it from the Windows Installer project. In real life, you can just press the F5 key to run the application and a new instance of the Visual Studio .NET IDE starts. After the new IDE instance is open, you can test your add-in. When you close the IDE instance, the add-in project goes out of Debug mode and you can modify your code. This is very handy when it comes to debugging: You can simply step into the add-in code from the other IDE instance.

You're going to install the add-in from the Windows Installer project just to see how the whole process works:

1.
Right-click on the FirstAddin project and select Build. Make sure that there are no errors in the Output window.

2.
Next, right-click on the FirstAddin_Setup project and select Build from the contextual menu. This creates the Windows Installer MSI package for you.

3.
When the build completes, right-click on the installer project and select Install from the contextual menu. This starts the Windows Installer Wizard that an end user would normally see. Go through the steps of installing the add-in on your computer. When the add-in is installed, all the necessary Registry entries are made for the COM interop, and the application directory where your add-in will exist is created.

Now you can test your add-in. Open a new instance of Visual Studio .NET and create a Windows Forms application. You can leave the default name. After the application is created and Visual Studio .NET is open, select the Tools menu. Your add-in should load, as Figure 17.12 demonstrates.

Figure 17.12. Your first add-in loaded in the Tools menu.


Next, click the menu item. You should see your ToolboxTest form pop up. Click the Show Toolbox Items buttons and you'll get a listing of your Toolbox tabs, as shown in Figure 17.13.

Figure 17.13. Getting a list of Toolbox tabs.


If you click the Add Toolbox Item button, a new tab is added and the code snippets you added are added as tab items. Notice that the item you added as HTML isn't available. That's because you aren't viewing an HTML file in the Code Editor. Figure 17.14 is what you see after dragging the Code Snippet item into the Code Editor.

Figure 17.14. The new Toolbox tab added with code snippets.


You've now successfully created your first add-in, attached to the IDE, and worked with the Toolbox.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.108.86