|
2 | 2 | title: "Creating a Multi-Instance Tool Window | Microsoft Docs"
|
3 | 3 | ms.date: "11/04/2016"
|
4 | 4 | ms.topic: "conceptual"
|
5 |
| -helpviewer_keywords: |
| 5 | +helpviewer_keywords: |
6 | 6 | - "multi"
|
7 | 7 | - "tool windows"
|
8 | 8 | ms.assetid: 4a7872f1-acc9-4f43-8932-5a526b36adea
|
9 | 9 | author: "gregvanl"
|
10 | 10 | ms.author: "gregvanl"
|
11 | 11 | manager: jillfra
|
12 |
| -ms.workload: |
| 12 | +ms.workload: |
13 | 13 | - "vssdk"
|
14 | 14 | ---
|
15 | 15 | # Create a multi-instance tool window
|
16 |
| -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. |
17 |
| - |
18 |
| - 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. |
19 |
| - |
20 |
| -## Create a basic (single-instance) tool window |
21 |
| - |
22 |
| -1. Create a project named **MultiInstanceToolWindow** using the VSIX template, and add a custom tool window item template named **MIToolWindow**. |
23 |
| - |
| 16 | +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. |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +## Create a basic (single-instance) tool window |
| 21 | + |
| 22 | +1. Create a project named **MultiInstanceToolWindow** using the VSIX template, and add a custom tool window item template named **MIToolWindow**. |
| 23 | + |
24 | 24 | > [!NOTE]
|
25 |
| - > For more information about creating an extension with a tool window, see [Create an extension with a tool window](../extensibility/creating-an-extension-with-a-tool-window.md). |
26 |
| - |
27 |
| -## Make a tool window multi-instance |
28 |
| - |
29 |
| -1. Open the *MIToolWindowPackage.cs* file and find the `ProvideToolWindow` attribute. and the `MultiInstances=true` parameter, as shown in the following example: |
30 |
| - |
31 |
| - ```csharp |
32 |
| - [PackageRegistration(UseManagedResourcesOnly = true)] |
33 |
| - [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About |
34 |
| - [ProvideMenuResource("Menus.ctmenu", 1)] |
35 |
| - [ProvideToolWindow(typeof(MultiInstanceToolWindow.MIToolWindow), MultiInstances = true)] |
36 |
| - [Guid(MIToolWindowPackage.PackageGuidString)] |
37 |
| - public sealed class MIToolWindowPackage : Package |
38 |
| - {. . .} |
39 |
| - ``` |
40 |
| - |
41 |
| -2. In the *MIToolWindowCommand.cs* file, find the `ShowToolWindos()` method. In this method, call the <xref:Microsoft.VisualStudio.Shell.Package.FindToolWindow%2A> method and set its `create` flag to `false` so that it will iterate through existing tool window instances until an available `id` is found. |
42 |
| - |
43 |
| -3. 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 its `create` flag to `true`. |
44 |
| - |
45 |
| - By default, the value of the `id` parameter of the <xref:Microsoft.VisualStudio.Shell.Package.FindToolWindow%2A> method is `0`. This value makes a single-instance tool window. For more than one instance to be hosted, every instance must have its own unique `id`. |
46 |
| - |
47 |
| -4. 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. |
48 |
| - |
49 |
| -5. 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 the `ShowToolWindow` method to create multiple instances. |
50 |
| - |
51 |
| - ```csharp |
52 |
| - private void ShowToolWindow(object sender, EventArgs e) |
53 |
| - { |
54 |
| - for (int i = 0; i < 10; i++) |
55 |
| - { |
56 |
| - ToolWindowPane window = this.package.FindToolWindow(typeof(MIToolWindow), i, false); |
57 |
| - if (window == null) |
58 |
| - { |
59 |
| - // Create the window with the first free ID. |
60 |
| - window = (ToolWindowPane)this.package.FindToolWindow(typeof(MIToolWindow), i, true); |
61 |
| - if ((null == window) || (null == window.Frame)) |
62 |
| - { |
63 |
| - throw new NotSupportedException("Cannot create tool window"); |
64 |
| - } |
65 |
| - |
66 |
| - IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame; |
67 |
| - Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show()); |
68 |
| - break; |
69 |
| - } |
70 |
| - } |
71 |
| - } |
72 |
| - ``` |
| 25 | + > For more information about creating an extension with a tool window, see [Create an extension with a tool window](../extensibility/creating-an-extension-with-a-tool-window.md). |
| 26 | +
|
| 27 | +## Make a tool window multi-instance |
| 28 | + |
| 29 | +1. Open the *MIToolWindowPackage.cs* file and find the `ProvideToolWindow` attribute. and the `MultiInstances=true` parameter, as shown in the following example: |
| 30 | + |
| 31 | + ```csharp |
| 32 | + [PackageRegistration(UseManagedResourcesOnly = true)] |
| 33 | + [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About |
| 34 | + [ProvideMenuResource("Menus.ctmenu", 1)] |
| 35 | + [ProvideToolWindow(typeof(MultiInstanceToolWindow.MIToolWindow), MultiInstances = true)] |
| 36 | + [Guid(MIToolWindowPackage.PackageGuidString)] |
| 37 | + public sealed class MIToolWindowPackage : Package |
| 38 | + {. . .} |
| 39 | + ``` |
| 40 | + |
| 41 | +2. In the *MIToolWindowCommand.cs* file, find the `ShowToolWindos()` method. In this method, call the <xref:Microsoft.VisualStudio.Shell.Package.FindToolWindow%2A> method and set its `create` flag to `false` so that it will iterate through existing tool window instances until an available `id` is found. |
| 42 | + |
| 43 | +3. 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 its `create` flag to `true`. |
| 44 | + |
| 45 | + By default, the value of the `id` parameter of the <xref:Microsoft.VisualStudio.Shell.Package.FindToolWindow%2A> method is `0`. This value makes a single-instance tool window. For more than one instance to be hosted, every instance must have its own unique `id`. |
| 46 | + |
| 47 | +4. 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. |
| 48 | + |
| 49 | +5. 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 the `ShowToolWindow` method to create multiple instances. |
| 50 | + |
| 51 | + ```csharp |
| 52 | + private void ShowToolWindow(object sender, EventArgs e) |
| 53 | + { |
| 54 | + for (int i = 0; i < 10; i++) |
| 55 | + { |
| 56 | + ToolWindowPane window = this.package.FindToolWindow(typeof(MIToolWindow), i, false); |
| 57 | + if (window == null) |
| 58 | + { |
| 59 | + // Create the window with the first free ID. |
| 60 | + window = (ToolWindowPane)this.package.FindToolWindow(typeof(MIToolWindow), i, true); |
| 61 | + if ((null == window) || (null == window.Frame)) |
| 62 | + { |
| 63 | + throw new NotSupportedException("Cannot create tool window"); |
| 64 | + } |
| 65 | + |
| 66 | + IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame; |
| 67 | + Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show()); |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + ``` |
0 commit comments