How to: Open a Tool Window Programmatically

Tool windows are typically opened by clicking a command on a menu, or by pressing an equivalent keyboard shortcut. However, you might have to open a tool window programmatically, like the command handler does.

To open a tool window in the managed VSPackage that provides it, use FindToolWindow. To open a tool window in another VSPackage, use FindToolWindow. In either case, the tool window is created as required.

The following code is taken from the C# Reference.ToolWindow sample.

To open a tool window programmatically

  1. Create the tool window pane, frame, and the VSPackage that implements them. For more information, see How to: Create a Tool Window.

  2. Register the tool window with Visual Studio by adding the ProvideToolWindowAttribute to the VSPackage that provides it.

    [ProvideToolWindow(typeof(MyToolWindow), Style = VsDockStyle.Tabbed, Window = "3ae79031-e1bc-11d0-8f78-00a0c9110057")]
    [ProvideMenuResource(1000, 1)]
    [PackageRegistration(UseManagedResourcesOnly = true)]
    public sealed class PackageToolWindow : Package

    This registers the tool window PersistedWindowPane to be opened as docked to Solution Explorer. For more information, see How to: Register a Tool Window.

  3. Use FindToolWindow to find the tool window pane or to create it if it does not already exist.

    // Get the (only) instance of this tool window.
    // The last flag is set to true so that if the tool window does not exists it will be created.
    ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true);
    if ((window == null) || (window.Frame == null))
        throw new NotSupportedException(Resources.CanNotCreateWindow);
  4. Get the tool window frame from the tool window pane.

    IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
  5. Show the tool window.


Community Additions