How to: Open a Tool Window Programmatically (C#)
Tool windows are typically opened from a command on a menu, or an equivalent keyboard shortcut. At times, however, you might need to open a tool window programmatically, like the command handler does.
Use FindToolWindow to open a tool window in the managed VSPackage that provides it. To open a tool window in another VSPackage, use FindToolWindow. In either case, the tool window is created as needed.
The code in the following procedure is taken from the C# Reference.ToolWindow sample.
To open a tool window programmatically
Create the tool window pane, frame, and the VSPackage that implements them. For more information, see How to: Create a Tool Window (C#).
Register the tool window with Visual Studio by adding the ProvideToolWindowAttribute to the VSPackage that provides it.
[MsVsShell.ProvideToolWindow(typeof(PersistedWindowPane), Style = MsVsShell.VsDockStyle.Tabbed, Window = "3ae79031-e1bc-11d0-8f78-00a0c9110057")] [MsVsShell.ProvideMenuResource(1000, 1)] [MsVsShell.DefaultRegistryRoot(@"Software\Microsoft\VisualStudio\8.0Exp")] [MsVsShell.PackageRegistration(UseManagedResourcesOnly = true)] [Guid("01069CDD-95CE-4620-AC21-DDFF6C57F012")] public class PackageToolWindow : MsVsShell.Package {
This registers the tool window PersistedWindowPane to be opened docked to the Solution Explorer. For more information, see How to: Register a Tool Window (C#).
Use FindToolWindow to find the tool window pane or to create it if it does not already exist.
// Get the 1 (index 0) and only instance of our tool window. // If it does not already exist it will get created. MsVsShell.ToolWindowPane pane = this.FindToolWindow(typeof(PersistedWindowPane), 0, true); if (pane == null) {
Get the tool window frame from the tool window pane.
IVsWindowFrame frame = pane.Frame as IVsWindowFrame; if (frame == null) {
Show the tool window.
// Bring the tool window to the front and give it focus ErrorHandler.ThrowOnFailure(frame.Show());