Click here to Skip to main content
Click here to Skip to main content

Creating a tool window add-in with Visual Studio 2005

, 16 Jan 2007
Rate this:
Please Sign up or sign in to vote.
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.

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:

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:

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.

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.

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)

About the Author

M Harris
Software Developer
Australia Australia
Mark spends his spare time working on his radio control planes, helicopters and trucks. He devises new ways to make them crash faster and easier than ever before. Mark has progressed this joy of destroying nice toys into build UAV's - which can crash themselves with, or without user input.
 
Mark enjoys all aspects of C#, specifically windows programming and regular expressions.

Comments and Discussions

 
GeneralMy vote of 4 PinmemberMember 899956419-May-12 7:56 
QuestionHow to chage the default Ico in VS.NET menu? Pinmemberxibeifeijian22-Apr-07 23:35 
QuestionWhat's wrong with official Microsoft guidlines? PinmembereisernWolf22-Jan-07 20:16 
AnswerRe: What's wrong with official Microsoft guidlines? PinmemberM Harris22-Jan-07 21:04 
GeneralRe: What's wrong with official Microsoft guidlines? Pinmemberredjoy23-Jan-07 3:43 
GeneralRe: What's wrong with official Microsoft guidlines? PinmemberM Harris23-Jan-07 12:14 
GeneralRe: What's wrong with official Microsoft guidlines? Pinmembershooterlily24-Jan-07 22:09 
GeneralRe: What's wrong with official Microsoft guidlines? Pinmemberjunlidk24-Feb-07 3:06 
AnswerRe: What's wrong with official Microsoft guidlines? PinprotectorMarc Clifton23-Jan-07 12:21 
GeneralRe: What's wrong with official Microsoft guidlines? [modified] PinmembereisernWolf26-Jan-07 18:44 
GeneralRe: What's wrong with official Microsoft guidlines? Pinmemberxibeifeijian27-Mar-07 17:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 16 Jan 2007
Article Copyright 2007 by M Harris
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid