Dotneteers.net
All for .net, .net for all!

LVN! Sidebar #3 - Simplifying tool window declaration

When you create a simple tool window with the VSPackage wizard, it creates a class derived from ToolWindowPane for you that delegates the user interface responsibility to a user control. The following code is one example:

[Guid("4469031d-23e0-483c-8566-ce978f6c9a6f")]

public class MyToolWindow : ToolWindowPane

{

  private MyControl control;

 

  public MyToolWindow() :  base(null)

  {

    this.Caption = Resources.ToolWindowTitle;

    this.BitmapResourceID = 301;

    this.BitmapIndex = 1;

    control = new MyControl();

  }

 

  override public IWin32Window Window

  {

    get

    {

      return (IWin32Window)control;

    }

  }

}

This model for tool window declaration is quite simple. Any time you use the pattern VSPackage wizard generates for you, you have to create the basic chunk of tool window code like the one above. If you do not want to repeat the pattern you can extract the stereotype behavior into a separate class. In this blog I will show you how.

Modified tool window declaration pattern

The pattern above can be written much shorter, one way is like this:

[Guid("d3533dc0-583a-43e4-be6c-0107b5616b0b")]

[InitialCaption("$ToolWindowTitle")]

[BitmapResourceId(301)]

public class MyToolWindow : VsxToolWindowPane<MyPackage, MyControl>

{

  // --- The class body is really empty...

}

If all your tool window logic is embedded into the MyControl user control (in case of simple tool windows it can be so), that’s all you have to put into the code to fully generate the tool window. Of course you have to define MyControl.

From the code above you can see that the magic is within the VsxToolWindowPane generic class and in the attributes decorating MyToolWindow. In the remaining part of this post I show you how the “magic” is done.

The VsxToolWindowPane class

Inside its body VsxToolWindowPane uses the same pattern as I showed you in the introduction part of this blog. The code of the class is quite simple:

public abstract class VsxToolWindowPane<TPackage, TUIControl> : ToolWindowPane

  where TPackage: Package

  where TUIControl: UserControl, new()

{

  private readonly TUIControl _UIControl;

 

  protected VsxToolWindowPane() : base(null)

  {

    // --- Obtain attributes of the class

    foreach (object attr in GetType().GetCustomAttributes(false))

    {

      InitialCaptionAttribute captionAttr = attr as InitialCaptionAttribute;

      if (captionAttr != null)

      {

        Caption = StringResolver<TPackage>.Resolve(captionAttr.Value);

        continue;

      }

      BitmapResourceIdAttribute resIdAttr = attr as BitmapResourceIdAttribute;

      if (resIdAttr != null)

      {

        BitmapResourceID = resIdAttr.ResourceId;

        BitmapIndex = resIdAttr.BitmapIndex;

        continue;

      }

    }

    // --- Create the user control

    _UIControl = new TUIControl();

  }

 

  public TUIControl UIControl

  {

    get { return _UIControl; }

  }

 

  public sealed override IWin32Window Window

  {

    get { return _UIControl; }

  }

}

VsxToolWindowPane uses two type arguments. TPackage represents the package owning the tool window; TUIControl is a placeholder for the user control implementing the user interface of the tool window.

The default constructor scans through the attributes decorating the VsxToolWindowPane derived class and recognizes the InitialCaption and BitmapResourceId attributes. These attributes are used to set up the tool window’s appearance. The constructor also creates an instance of the user control representing the tool window’s UI; this instance can be accessed through the UIControl property. The overridden Window property tells to the pane which Win32 window handle represents the UI.

One subtle detail in the code above is the usage of the StringResolver type. This type resolves resource strings in the VSPackage.resx or Resources.resx files. To look for details, read the LVN! Sidebar #2.

Decorator attributes

The definition of tool window attributes is quite straightforward:

[AttributeUsage(AttributeTargets.Class)]

public sealed class InitialCaptionAttribute: StringAttribute

{

  public InitialCaptionAttribute(string value) : base(value)

  {

  }

}

 

[AttributeUsage(AttributeTargets.Class)]

public sealed class BitmapResourceIdAttribute: Attribute

{

  private readonly int _ResourceId;

  private readonly int _BitmapIndex;

 

  public BitmapResourceIdAttribute(int resourceId):

    this (resourceId, 1)

  {

  }

 

  public BitmapResourceIdAttribute(int resourceId, int bitmapIndex)

  {

    _ResourceId = resourceId;

    _BitmapIndex = bitmapIndex;

  }

 

  public int ResourceId

  {

    get { return _ResourceId; }

  }

 

  public int BitmapIndex

  {

    get { return _BitmapIndex; }

  }

}

Sample tool window definition

To demonstrate tool window definition I will you a short sample. Let’s assume that our tool window embeds a WebBrowser control and when showing up automatically navigates to a concrete page. To create the tool window, first define a user control with the name of LearnVSXNowToolControl and dock a WebBrowser control to fill the whole user control. Name it Browser. Add a public Navigate method to the user control. After adding it, its code should look like this below:

public partial class LearnVSXNowToolControl : UserControl

{

  public LearnVSXNowToolControl()

  {

    InitializeComponent();

  }

 

  public void Navigate(string url)

  {

    Browser.Navigate(url);

  }

}

Now we can create the tool window with the pattern a demonstrated above. Name it to LearnVSXNowToolWindow, and add the following code to it:

[Guid("d3533dc0-583a-43e4-be6c-0107b5616b0b")]

[InitialCaption("$ToolWindowTitle")]

[BitmapResourceId(301)]

public class LearnVSXNowToolWindow :

  VsxToolWindowPane<HowToPackage, LearnVSXNowToolControl>

{

  public LearnVSXNowToolWindow()

  {

    UIControl.Navigate("http://www.codeplex.com/LearnVSXNow");

  }

}

Our tool window uses the firs bitmap in the bitmap strip with resource ID 301. The initial caption of the tool window is taken from the ToolWindowTitle property of the Resources.resx file. To add functionality to the window, we create a constructor that navigates to the LearnVSXNow! home page.

Using the tool window

Even if you declared the tool window with the VsxToolWindowPane<,> class, do not forget that the tool window should be registered with the ProvideToolWindow attribute decorating your package and you have to write code for showing up the tool window. For details about tool windows, see LearnVSXNow! - Part #4.


Posted Mar 26 2008, 08:13 AM by inovak
Filed under: ,

Comments

Clomiphene 50 mg wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Mon, Feb 25 2013 10:57

C7GNsq Im grateful for the article.Thanks Again. Really Great.

get me more followers twitter wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Tue, Feb 26 2013 19:24

Major thanks for the article post.Thanks Again. Cool.

how make hair grow faster wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Tue, Feb 26 2013 21:04

Thank you for your article. Want more.

seo package wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Tue, Feb 26 2013 22:24

Thank you for your blog article.Thanks Again. Really Great.

earned money wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Tue, Feb 26 2013 22:44

wow, awesome article post.Much thanks again. Much obliged.

Brandable Domains wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Tue, Feb 26 2013 23:08

I truly appreciate this blog.Really looking forward to read more. Awesome.

instagram followers website wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 0:18

Thanks-a-mundo for the article.Really thank you! Really Great.

Digital Cameras and Camcorders wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 0:19

A round of applause for your blog post.Really looking forward to read more. Really Great.

colonie de vacances wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 1:01

Thanks so much for the blog.Really looking forward to read more. Much obliged.

vagas wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 1:54

Very neat blog post.Thanks Again. Want more.

buy instagram followers wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 2:09

Really appreciate you sharing this blog post. Will read on...

click here now to join wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 2:53

Say, you got a nice article post.Much thanks again. Much obliged.

Existing businesses for sale wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 3:31

I really enjoy the blog.Really thank you!

kim kardashian video wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 4:02

Appreciate you sharing, great blog post.Thanks Again. Cool.

Kim Kardashian video wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 4:46

Enjoyed every bit of your blog article.Thanks Again. Really Cool.

the trick photography and special effects wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 5:10

Very informative blog.Much thanks again. Will read on...

real instagram camera wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 5:56

Fantastic blog post.Really thank you! Awesome.

new dating sites wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 6:41

Muchos Gracias for your post. Great.

get more clients wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 6:49

Im obliged for the post.Really thank you! Will read on...

buy 100 instagram likes wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 7:50

I really liked your article post.Really looking forward to read more. Great.

nikon d3200 price in chennai wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 8:28

Really enjoyed this article post.Thanks Again. Fantastic.

furniture plans wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 8:38

Thanks so much for the blog. Fantastic.

resort wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 9:49

Hey, thanks for the article.Thanks Again. Fantastic.

idaniko varos wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 10:08

Thanks-a-mundo for the article post.Thanks Again. Really Great.

buy real instagram followers wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 10:35

Appreciate you sharing, great blog post.Thanks Again. Great.

kerala hotel booking wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 11:44

Im thankful for the article post.Thanks Again. Great.

facebook subscribe wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 11:47

I am so grateful for your article. Great.

T mobile wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 12:30

Major thanks for the post.Thanks Again. Will read on...

earn extra money wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 13:39

Thanks-a-mundo for the article post. Want more.

sbobet wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 14:25

Thanks for the blog article.Much thanks again. Will read on...

roofing company dallas tx wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 15:33

This is one awesome blog article.Much thanks again.

how to get rid of a yeast infection quickly wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 16:20

Thanks so much for the blog article.Much thanks again.

nlock motorola wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 18:16

Wow, great blog post.Much thanks again. Really Cool.

buy oil paintings online wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 19:26

Great article post. Really Cool.

racehorse partnership wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 20:14

Muchos Gracias for your blog article.Really thank you!

how to be a personal assistant wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 21:25

Appreciate you sharing, great article.Really thank you! Will read on...

pianists new york wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 22:16

I think this is a real great article post.Much thanks again. Much obliged.

Charlotte Bax wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Wed, Feb 27 2013 23:25

I cannot thank you enough for the post.Thanks Again. Great.

Kirkland Locksmith wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Thu, Feb 28 2013 5:27

Muchos Gracias for your article.Really thank you! Really Great.

advertising book wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Thu, Feb 28 2013 9:29

Really informative post.Really thank you! Really Cool.

Vpn wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Thu, Feb 28 2013 20:22

Really appreciate you sharing this blog article.Much thanks again.

ourmeds wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Thu, Feb 28 2013 21:58

Really enjoyed this blog article.Really looking forward to read more. Really Great.

thy ucak bileti|thy ucak bileti wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 12:04

Very neat blog post. Fantastic.

GeForce GTX 690 wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 12:24

I am so grateful for your post.Really thank you! Great.

escort london wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 13:59

Really enjoyed this article.Really thank you! Cool.

adjustable dumbbells wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 15:35

Im obliged for the article post. Really Great.

social bookmarking wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 17:02

I truly appreciate this blog.Much thanks again. Cool.

article producer tool wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 17:03

This is one awesome blog post.

amazing steals wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 17:10

This is one awesome blog article.

affiliate marketing|make 100 dollars a day wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 18:46

Thanks a lot for the article post.Really looking forward to read more. Keep writing.

dublin pass discount wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 18:47

Im thankful for the article.Much thanks again. Keep writing.

how to light video wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 19:07

Fantastic blog.Really looking forward to read more. Much obliged.

cardiology equipment wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 20:33

I really liked your blog. Much obliged.

Network marketing iniziativa wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 21:13

Thank you ever so for you blog post.Thanks Again. Really Cool.

watch here wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 21:54

I appreciate you sharing this blog.Really thank you! Great.

Ferienwohnung cuxhaven wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 22:16

Thanks for the article. Great.

halovar wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 23:58

I loved your article post.Thanks Again. Fantastic.

Riu Hotels Special wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Fri, Mar 1 2013 23:59

Very informative blog article.Really looking forward to read more. Much obliged.

oxyelite usplabs wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 1:43

Enjoyed every bit of your article post. Want more.

lipo 6 black wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 3:26

I am so grateful for your blog.Really looking forward to read more. Keep writing.

Lina Aishima wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 3:29

I really like and appreciate your blog post.Really thank you! Want more.

specialty pharmacy wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 4:09

I appreciate you sharing this blog. Really Cool.

make your own website wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 5:10

Major thanks for the article.Much thanks again. Want more.

kitchens manchetser wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 6:12

I really enjoy the blog post.Thanks Again. Really Great.

cheap car insurance wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 6:54

I truly appreciate this article post.Really looking forward to read more. Really Great.

extensionfervor wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 7:37

Great article post. Fantastic.

Get A Payday Loan wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 8:38

Appreciate you sharing, great blog post.Thanks Again. Really Great.

seo services adelaide wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 10:24

Thank you ever so for you article.Really thank you! Great.

gold buyers wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 12:12

Very neat article post. Really Great.

Get Twitter followers wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 12:54

Thanks again for the post. Want more.

green coffee bean max reviews wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 13:59

Fantastic post.Really looking forward to read more.

Creative Domain Names wrote re: LVN! Sidebar #3 - Simplifying tool window declaration
on Sat, Mar 2 2013 14:32

I cannot thank you enough for the blog article.Really looking forward to read more. Great.