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.
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.
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.
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.
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.
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.
Object | Description |
---|---|
Toolbox | Represents the Toolbox |
ToolboxTabs | Represents all the tabs in the Toolbox |
ToolboxTab | Represents a tab in the Toolbox |
ToolboxItems | Returns a collections of all the items in a tab in the Toolbox |
ToolboxItem | Represents 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.
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:
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.
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.
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.
You've now successfully created your first add-in, attached to the IDE, and worked with the Toolbox.
3.12.108.86