Click here to Skip to main content
15,884,629 members
Articles / Programming Languages / C#
Article

Creating a tool window add-in with Visual Studio 2005

Rate me:
Please Sign up or sign in to vote.
4.79/5 (17 votes)
16 Jan 2007CPOL3 min read 64.1K   626   42   11
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)


Written By
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 Pin
Member 899956419-May-12 7:56
Member 899956419-May-12 7:56 
QuestionHow to chage the default Ico in VS.NET menu? Pin
xibeifeijian22-Apr-07 23:35
xibeifeijian22-Apr-07 23:35 
QuestionWhat's wrong with official Microsoft guidlines? Pin
eisernWolf22-Jan-07 20:16
eisernWolf22-Jan-07 20:16 
AnswerRe: What's wrong with official Microsoft guidlines? Pin
M Harris22-Jan-07 21:04
M Harris22-Jan-07 21:04 
My aim was to create an easy to follow guide to create a tool window for visual studio. There are no other such articles purely on performing such a task on this site.

The MSDN documentation does not demonstrate how to create menu item to display the tool window - just how to make it load when your application starts. Their way is not going to be suitable for many users.

This article is designed for beginners who are looking for an easy, consise way to get their toolwindow addin happening.

--
Real programmers don't comment their code. It was hard to write, it should be hard to understand.

GeneralRe: What's wrong with official Microsoft guidlines? Pin
redjoy23-Jan-07 3:43
redjoy23-Jan-07 3:43 
GeneralRe: What's wrong with official Microsoft guidlines? Pin
M Harris23-Jan-07 12:14
M Harris23-Jan-07 12:14 
GeneralRe: What's wrong with official Microsoft guidlines? Pin
shooterlily24-Jan-07 22:09
shooterlily24-Jan-07 22:09 
GeneralRe: What's wrong with official Microsoft guidlines? Pin
jldk24-Feb-07 3:06
jldk24-Feb-07 3:06 
AnswerRe: What's wrong with official Microsoft guidlines? Pin
Marc Clifton23-Jan-07 12:21
mvaMarc Clifton23-Jan-07 12:21 
GeneralRe: What's wrong with official Microsoft guidlines? [modified] Pin
eisernWolf26-Jan-07 18:44
eisernWolf26-Jan-07 18:44 
GeneralRe: What's wrong with official Microsoft guidlines? Pin
xibeifeijian27-Mar-07 17:18
xibeifeijian27-Mar-07 17:18 

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

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