| title | Creating a Multi-Instance Tool Window | ||
|---|---|---|---|
| description | Learn how to modify a tool window so that multiple instances of it can be open simultaneously. By default, tool windows can have only one instance open. | ||
| ms.date | 11/04/2016 | ||
| ms.topic | how-to | ||
| helpviewer_keywords |
|
||
| author | tinaschrepfer | ||
| ms.author | tinali | ||
| ms.subservice | extensibility-integration |
You can program a tool window so that multiple instances of it can be open simultaneously. By default, tool windows can have only one instance open.
When you use a multi-instance tool window, you can show several related sources of information at the same time. For example, you could put a multi-line xref:System.Windows.Forms.TextBox control in a multi-instance tool window so that several code snippets are simultaneously available during a programming session. Also, for example, you could put a xref:System.Windows.Forms.DataGrid control and a drop-down list box in a multi-instance tool window so that several real-time data sources can be tracked simultaneously.
-
Create a project named MultiInstanceToolWindow using the VSIX template, and add a custom tool window item template named MIToolWindow.
[!NOTE] For more information about creating an extension with a tool window, see Create an extension with a tool window.
-
Open the MIToolWindowPackage.cs file and find the
ProvideToolWindowattribute. and theMultiInstances=trueparameter, as shown in the following example:[PackageRegistration(UseManagedResourcesOnly = true)] [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About [ProvideMenuResource("Menus.ctmenu", 1)] [ProvideToolWindow(typeof(MultiInstanceToolWindow.MIToolWindow), MultiInstances = true)] [Guid(MIToolWindowPackage.PackageGuidString)] public sealed class MIToolWindowPackage : Package {. . .}
-
In the MIToolWindowCommand.cs file, find the
ShowToolWindos()method. In this method, call the xref:Microsoft.VisualStudio.Shell.Package.FindToolWindow%2A method and set itscreateflag tofalseso that it will iterate through existing tool window instances until an availableidis found. -
To create a tool window instance, call the xref:Microsoft.VisualStudio.Shell.Package.FindToolWindow%2A method and set its
idto an available value and itscreateflag totrue.By default, the value of the
idparameter of the xref:Microsoft.VisualStudio.Shell.Package.FindToolWindow%2A method is0. This value makes a single-instance tool window. For more than one instance to be hosted, every instance must have its own uniqueid. -
Call the xref:Microsoft.VisualStudio.Shell.Interop.IVsWindowFrame.Show%2A method on the xref:Microsoft.VisualStudio.Shell.Interop.IVsWindowFrame object that is returned by the xref:Microsoft.VisualStudio.Shell.ToolWindowPane.Frame%2A property of the tool window instance.
-
By default, the
ShowToolWindowmethod that is created by the tool window item template creates a single-instance tool window. The following example shows how to modify theShowToolWindowmethod to create multiple instances.private void ShowToolWindow(object sender, EventArgs e) { for (int i = 0; i < 10; i++) { ToolWindowPane window = this.package.FindToolWindow(typeof(MIToolWindow), i, false); if (window == null) { // Create the window with the first free ID. window = (ToolWindowPane)this.package.FindToolWindow(typeof(MIToolWindow), i, true); if ((null == window) || (null == window.Frame)) { throw new NotSupportedException("Cannot create tool window"); } IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame; Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show()); break; } } }