VSIX Winforms Toolpane
The other day I wanted to create a Visual Studio Toolpane window and I wanted to create it using Winforms (by default, the toolpanes use WPF).
After much research, I got it to work. These are the steps I followed:
I've tested these steps with Visual Studio 2015 & Visual Studio 2017.
- Create a new Extensibility project
- In Visual Studio, from the File menu, select New --> Project.
- In the New Project dialog, one the left, under Visual C#, select Extensibility.
- On the right, select VSIX Project.
- Give the project a name. In this example, I'm calling it MySampleVSIXProject
- Add a Visual Studio Package
- Right-click the project in Solution Explorer and select Add --> New Item
- In the Add New Item dialog, on the left, expand Extensibility and select VSPackage.
- On the right, select Custom Tool Window.
- Give the tool window a name. In this example, I'm calling it MySampleToolWindow.cs
- Add a Winforms User Control
- Right-click the project in Solution Explorer and select Add --> New Item
- In the Add New Item dialog, on the left, select Windows Forms
- On the right select User Control
- Give the User Control a name. In this example, I'm calling it MySampleUserControl.cs
- Drag a label to the control so we can see something when we run it. Change the Text property. In this example, I changed it to "I'm a Winforms Control".
- Substitute our Winform Control for the WPF Control
- Open the MySampleToolWindow.cs file. It should look something like this (I've removed the comments for simplicity):
- Add a field to the MySampleToolWindow class. Make it of type MySampleUserControl and name it control.
- Then comment out the line that says "this.Content = new MySampleToolWindowControl();"
- Under the commented out line, add a line to create an instance of our control.
- Add a using System.Windows.Forms; statement at the top of the code.
- Next, add this code to the class:
public class MySampleToolWindow : ToolWindowPane { public MySampleToolWindow() : base(null) { this.Caption = "MySampleToolWindow"; this.Content = new MySampleToolWindowControl(); } }
public class MySampleToolWindow : ToolWindowPane { public MySampleUserControl control; public MySampleToolWindow() : base(null) { this.Caption = "MySampleToolWindow"; //this.Content = new MySampleToolWindowControl(); this.control = new MySampleUserControl(); } }
public override IWin32Window Window { get { return (IWin32Window)control; } }It should now look like this:
public class MySampleToolWindow : ToolWindowPane { public MySampleUserControl control; public MySampleToolWindow() : base(null) { this.Caption = "MySampleToolWindow"; //this.Content = new MySampleToolWindowControl(); this.control = new MySampleUserControl(); } public override IWin32Window Window { get { return (IWin32Window)control; } } }
Now we can run the project. In the experimental version of Visual Studio that opens, from the View menu, under Other Windows, you should see our window ("MySampleToolWindow".) Click it to open our window. It opens in a window by itself.
That's great. But what if we want it to open already docked to the Solution Explorer panel?
That's easy. We just have to add some attributes. Open the MySampleToolWindowPackage.cs. On the MySampleToolWindowPackage class, find the ProvideToolWindow attribute. It should look like this:
[ProvideToolWindow(typeof(MySampleToolWindow))]
We'll add these Style and Windows attributes to it:
[ProvideToolWindow(typeof(MySampleToolWindow), Style = Microsoft.VisualStudio.Shell.VsDockStyle.Tabbed, Window = "3ae79031-e1bc-11d0-8f78-00a0c9110057"))]According to this MSDN page:
"The first named parameter is Style and its value is Tabbed, which means that the window will be a tab in an existing window. The docking position is specified by the Window parameter, in this case, the GUID of the Solution Explorer."
Now if we run it, it will show up next to the Solution Explorer:
At this point, you can optionally delete the MySampleToolWindowControl.xaml file. This is the WPF control that we're not using.
Comments