5

I have an extensibility project in Visual Studio and I need to use the event triggered when I change from one window to another in the visual studio editor, my problem:

I created a Tool Window that display some diagram, that diagram depend of an editable file, when I save the editable file my tool window updates the information, but when there is more than one editable file opened and I switch between them I want that the tool window updates the information as well. So:

I want to get the event triggered when I switch between windows, file or documents in Visual Studio so I can use it to execute the update code of my tool window. Is there something I can do about it?

I just read this question here but I didn't find a solution in there: Are there any document window focus events?

Community
  • 1
  • 1
Jack1987
  • 727
  • 1
  • 14
  • 26

1 Answers1

6

You can subscribe to the EnvDTE.WindowEvents.WindowActivated event:

using EnvDTE;
using Microsoft.VisualStudio.Shell;

private class MyClass
{
    private DTE dte;

    public MyClass()
    {
        dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
        dte.Events.WindowEvents.WindowActivated += OnWindowActivated;
    }

    private void OnWindowActivated(Window gotFocus, Window lostFocus)
    {
        throw new NotImplementedException();
    }
}

See for example the 1. Display document path of the active window in the status bar sample code.

Dan Bechard
  • 5,104
  • 3
  • 34
  • 51
Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66