Share via


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.

    <PackageRegistration(UseManagedResourcesOnly:=True), _
    InstalledProductRegistration("#110", "#112", "1.0", IconResourceID:=400), _
    ProvideMenuResource(1000, 1), _
    ProvideToolWindow(GetType(MyToolWindow), Style:=VsDockStyle.Tabbed, Window:="3ae79031-e1bc-11d0-8f78-00a0c9110057"), _
    DefaultRegistryRoot("Software\Microsoft\VisualStudio\8.0Exp"), _
    Guid("01069CDD-95CE-4620-AC21-DDFF6C57F012")> _
    Public NotInheritable Class PackageToolWindow
        Inherits Package
    
    [ProvideToolWindow(typeof(MyToolWindow), Style = VsDockStyle.Tabbed, Window = "3ae79031-e1bc-11d0-8f78-00a0c9110057")]
    [ProvideMenuResource(1000, 1)]
    [DefaultRegistryRoot(@"Software\Microsoft\VisualStudio\8.0Exp")]
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [Guid("01069CDD-95CE-4620-AC21-DDFF6C57F012")]
    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. 
    Dim window As ToolWindowPane = Me.FindToolWindow(GetType(MyToolWindow), 0, True)
    If (window Is Nothing) Or (window.Frame Is Nothing) Then 
        Throw New NotSupportedException(Resources.CanNotCreateWindow)
    End If
    
    // 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.

    Dim windowFrame As IVsWindowFrame = TryCast(window.Frame, IVsWindowFrame)
    
    IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
    
  5. Show the tool window.

    ErrorHandler.ThrowOnFailure(windowFrame.Show())
    
    ErrorHandler.ThrowOnFailure(windowFrame.Show());
    

See Also

Concepts

VSPackage Essentials