Logo
HOWTO: Host a Windows Forms usercontrol in a toolwindow from a Visual Studio package.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2010
Date: July 2014   Microsoft Visual Studio 2012
      Microsoft Visual Studio 2013
Introduction

When you create a toolwindow with the Visual Studio package wizard, a WPF usercontrol is hosted inside the toolwindow. This article explains how to change it to host a Windows Forms usercontrol instead.

More Information

To host a Windows Forms usercontrol inside the toolwindow generated by the Visual Studio package wizard follow these steps:

  • Remove the MyControl.xaml file.
  • Click the "Add" > "New Item..." menu, select "Windows Forms" > "User Control" and change its name to MyUserControl.cs (if using C#) or MyUserControl.vb (if using VB.NET).
  • In the MyToolWindow.cs or MyToolWindow.vb file assigning the base Content property to a new instance of this Windows Forms usercontrol wouldn't work (although it wouldn't cause an exception). Instead, change it to the following code:
Language: C#   Copy Code Copy Code (IE only)
public class MyToolWindow : ToolWindowPane
{
   private MyUserControl m_myUserControl;

   public MyToolWindow(): base(null)
   {
      this.Caption = Resources.ToolWindowTitle;
      this.BitmapResourceID = 301;
      this.BitmapIndex = 1;
            
      // This wouldn't work
      // base.Content = new MyControl();

      m_myUserControl = new MyUserControl();
   }

   public override System.Windows.Forms.IWin32Window Window
   {
      get
      {
         return m_myUserControl;
      }
   }
}
Language: VB.NET   Copy Code Copy Code (IE only)
Public Class MyToolWindow
   Inherits ToolWindowPane

   Private m_myUserControl As MyUserControl

   Public Sub New()

      MyBase.New(Nothing)
      Me.Caption = Resources.ToolWindowTitle
      Me.BitmapResourceID = 301
      Me.BitmapIndex = 1

      ' This wouldn't work
      ' Me.Content = New MyUserControl()

      m_myUserControl = New MyUserControl()

   End Sub

   Public Overrides ReadOnly Property Window As System.Windows.Forms.IWin32Window
      Get
         Return m_myUserControl
      End Get
   End Property

End Class


Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)