Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Creating a tool window add-in with Visual Studio 2005

4.79/5 (17 votes)
16 Jan 2007CPOL3 min read 1   626  
Shows you how to create a tool window add-in with Visual Studio 2005.

Sample Image - vstoolwindow.jpg

Introduction

The aim of this article is to show you how to create a Visual Studio .NET 2005 add-in. This add-in will contain a ToolWindow, and a menu item under the View menu to display that ToolWindow. Should the window be closed, the menu item can be used to display it again.

Creating the Project

First off, create a new Extensibility project, with a Visual Studio Add-in type. This will display the wizard to guide you though the project creation. During the wizard, you will need to choose the following options:

  • Programming language: C#

A tools menu item will be created. The add-in will load only when the user selects the Load checkbox within the Add-in Manager dialog box.

Image 2

The major page of the wizard is page 4, where you are able to select to create the tools menu item.

Adding a Tool Window Control

A tool window is a user control hosted within an ActiveX container, so go ahead and add a new user control to the project. Mine is called ToolWindowControl, and has a single button on it. You can make your control as complex as you wish; however, keep in mind the amount of real estate it consumes.

To gain access to the IDE's "object" with your control, create a private field on the control of type EnvDTE80.DTE2 called applicationObject. Then, create a get/set property for this field; the quickest way is to right click the field, under the Refactor menu, click Encapsulate Field, and then click OK. When the window is initialised, you will set this property.

Modifying the Add-in for the Tool Window

Almost every other tool window in the IDE is opened through the View menu. The wizard generated code will place an item on the Tools menu for us, we need to change its location. In Connect.cs, you will find the code that creates the menu item and handles it.

The OnConnection method contains the code for generating the menu item. To move it to the View menu, simply go through this method and replace any "Tools" you find with "View". All the important matches should be in the first try/catch block. Your code should end up like this:

C#
try
{
    ResourceManager resourceManager = 
        new ResourceManager("ToolWindowArticle.CommandBar", 
        Assembly.GetExecutingAssembly());
    CultureInfo cultureInfo = new 
        System.Globalization.CultureInfo(_applicationObject.LocaleID);
    string resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, 
                                        "View");
    toolsMenuName = resourceManager.GetString(resourceName);
}
catch
{
    toolsMenuName = "View";
}

I removed the comments that the wizard generated, to keep the code short.

Next, two private fields should be created within the class to hold the window and the control. Mine are as follows:

C#
private Window toolWindow;
private ToolWindowControl toolWindowControl;

As the tool window will not be created until after the IDE has loaded, the code should not reside within the same section of code as the menu item generation. At the end of the OnConnect method, add an else if statement with the condition: connectMode == ext_ConnectMode.ext_cm_AfterStartup. This condition makes sure that the code only gets executed after start-up. If you copy and paste the following code, you will need to update it to include your variables and a new GUID. This code creates the window and sets the ApplicationObject property on the control.

C#
else if (connectMode == ext_ConnectMode.ext_cm_AfterStartup)
{
    #region Load Tool Window
    object programmableObject = null;
    string guidString = "{9FFC9D9B-1F39-4763-A2AF-66AED06C799E}";
    Windows2 windows2 = (Windows2)_applicationObject.Windows;
    Assembly asm = Assembly.GetExecutingAssembly();
    toolWindow = windows2.CreateToolWindow2(_addInInstance, asm.Location, 
        "ToolWindowArticle.ToolWindowControl", 
        "Pirate Window", guidString, ref programmableObject);
    toolWindow.Visible = true;
    toolWindowControl = (ToolWindowControl)toolWindow.Object;
    toolWindowControl.ApplicationObject = _applicationObject;
    #endregion
}

Getting the Menu Item to Display the ToolWindow

The window will display when the add-in is loaded; however, there is no way to re-display the window after it has been closed. The first click on the menu item loads the add-in; however, further clicks will not make it display again. A quick addition to the Exec method will display it again each time the menu item is clicked. Under the if statement for your commandName, add the following.

C#
toolWindow.Visible = true;

Conclusion

This article has hopefully helped you create a Visual Studio add-in which displays a ToolWindow which is capable of interacting with the IDE. The View menu item which you created should re-display the window if it be closed.

History

  • 17/02/2007 - First edition.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)