2

So I've got two tool windows in my visual studio extension (package) and I'd like to open up the second window via a button on the first window.

I expected this to be explained here: "How to: Open a Tool Window Programmatically", but it wasn't.

Community
  • 1
  • 1
mYnDstrEAm
  • 751
  • 2
  • 8
  • 26

3 Answers3

5

You should use either Package.FindToolWindow or IVsUIShell.FindToolWindow to find or create a tool window.

If used from your own package (or if you have a reference to the package, just put it there instead of this):

private void OpenFromPackage()
{
    ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true); // True means: crate if not found. 0 means there is only 1 instance of this tool window
    if (null == window || null == window.Frame)
        throw new NotSupportedException("MyToolWindow not found");

    IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
    ErrorHandler.ThrowOnFailure(windowFrame.Show());
}

If you can't do it from your package, or don't have a reference to it, use IVSUIShell:

private void OpenWithIVsUIShell()
{
    IVsUIShell vsUIShell = (IVsUIShell)Package.GetGlobalService(typeof(SVsUIShell));
    Guid guid = typeof(MyToolWindow).GUID;
    IVsWindowFrame windowFrame;
    int result = vsUIShell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fFindFirst, ref guid, out windowFrame);   // Find MyToolWindow

    if (result != VSConstants.S_OK)
        result = vsUIShell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fForceCreate, ref guid, out windowFrame); // Crate MyToolWindow if not found

    if (result == VSConstants.S_OK)                                                                           // Show MyToolWindow
        ErrorHandler.ThrowOnFailure(windowFrame.Show());
 }
Shakaron
  • 1,027
  • 10
  • 24
  • Can you help me with this https://stackoverflow.com/questions/63180631/how-to-close-dispose-tool-window-when-visual-studio-solution-closed-and-create-o? – Diksha Mundhra Jul 30 '20 at 20:11
  • How to go about if I want to show the same window(1st tool window) with updated data from a click of a button in windows form( Ex: on click of login button in windows form pop up from 1st tool window). – Siddhi Kamat Jun 08 '21 at 15:12
1

When you create a new package with toolwindow support, you get a single toolwindow and a command that displays it. This command is handled in the package class with the ShowToolWindow method.

Examining that, you'll see that the base package object has a FindToolWindow method that you can use to find (and create if needed) any toolwindow you have implemented in your package. That FindToolWindow method is just a nice wrapper around the IVsUIShell.FindToolWindow method, which is what ultimately gets invoked when displaying any toolwindow.

So instead of using the old EnvDTE automation interface, I would recommend using the lower level services built into the actual package object.

Ed Dore
  • 2,013
  • 1
  • 10
  • 8
0

Here's how I solved it, the following code is the code-behind method of the button on the first window:

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var dte = Package.GetGlobalService(typeof(DTE)) as DTE;
            if (dte == null) return;

            var window = dte.Windows.Item("{WindowGUID}");
            window.Visible = true;
        }

You should find the "WindowGUID" in the Guids class and above the class of the ToolWindow.

mYnDstrEAm
  • 751
  • 2
  • 8
  • 26