5,276,801 members and growing! (16,831 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » How To     Intermediate License: The Code Project Open License (CPOL)

Creating a tool window add-in with Visual Studio 2005

By M Harris

Shows you how to create a tool window add-in with Visual Studio 2005.
C#, Windows, .NET, Visual Studio (VS2005, VS), Dev

Posted: 16 Jan 2007
Updated: 16 Jan 2007
Views: 15,862
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 5.50 Rating: 4.57 out of 5
0 votes, 0.0%
1
1 vote, 6.3%
2
1 vote, 6.3%
3
2 votes, 12.5%
4
12 votes, 75.0%
5

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


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 is working on connecting his radio control aircraft to his computer for live onboard video feed from which he can fly the plane. His computer controls the pan and tilt of the camera and using a joystick connected to the computer he flies the plane. This has proven to be a very efficient way to return a perfectly good plane back to basic parts.

Mark works for IT Vision in Perth, Western Australia during the week working on all sorts of exciting things in C#. After 5 years of commercial software development Mark has decided its about time he got some qualifications, therefore this year he is diving into academia and going to Murdoch University.
Occupation: Web Developer
Location: Australia Australia

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionHow to chage the default Ico in VS.NET menu?memberxibeifeijian0:35 23 Apr '07  
GeneralWhat's wrong with official Microsoft guidlines?membereisernWolf21:16 22 Jan '07  
GeneralRe: What's wrong with official Microsoft guidlines?memberM Harris22:04 22 Jan '07  
GeneralRe: What's wrong with official Microsoft guidlines?memberredjoy4:43 23 Jan '07  
GeneralRe: What's wrong with official Microsoft guidlines?memberM Harris13:14 23 Jan '07  
GeneralRe: What's wrong with official Microsoft guidlines?membershooterlily23:09 24 Jan '07  
GeneralRe: What's wrong with official Microsoft guidlines?memberjunlidk4:06 24 Feb '07  
GeneralRe: What's wrong with official Microsoft guidlines?supporterMarc Clifton13:21 23 Jan '07  
GeneralRe: What's wrong with official Microsoft guidlines? [modified]membereisernWolf19:44 26 Jan '07  
GeneralRe: What's wrong with official Microsoft guidlines?memberxibeifeijian18:18 27 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Jan 2007
Editor: Smitha Vijayan
Copyright 2007 by M Harris
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project