title | description | ms.date | ms.topic | helpviewer_keywords | author | ms.author | manager | ms.subservice | ||
---|---|---|---|---|---|---|---|---|---|---|
Creating a Multi-Instance Tool Window | 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. | 11/04/2016 | how-to |
| maiak | maiak | mijacobs | 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
ProvideToolWindow
attribute. and theMultiInstances=true
parameter, 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)]publicsealedclassMIToolWindowPackage: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 itscreate
flag tofalse
so that it will iterate through existing tool window instances until an availableid
is found.To create a tool window instance, call the xref:Microsoft.VisualStudio.Shell.Package.FindToolWindow%2A method and set its
id
to an available value and itscreate
flag totrue
.By default, the value of the
id
parameter 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
ShowToolWindow
method that is created by the tool window item template creates a single-instance tool window. The following example shows how to modify theShowToolWindow
method to create multiple instances.privatevoidShowToolWindow(objectsender,EventArgse){for(inti=0;i<10;i++){ToolWindowPanewindow=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)){thrownewNotSupportedException("Cannot create tool window");}IVsWindowFramewindowFrame=(IVsWindowFrame)window.Frame;Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());break;}}}