Logo
BUG: DTE.ActiveDocument.ActiveWindow does not return the active document window if toolwindow active

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: March 2013   Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
Introduction

If you have a document with two windows open (such as a code window and a designer window) and you call DTE.ActiveDocument.ActiveWindow while a toolwindow is active, it returns the first window which was opened for the document, not the active one.

Cause

This is a bug of Visual Studio.

More Information

Steps to reproduce the problem:

  • Create an add-in with the code below. Do not mark the add-in to load on startup:
Language: C#   Copy Code Copy Code (IE only)
private DTE2 _applicationObject;
private AddIn _addInInstance;
      
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    switch (connectMode)
    {
        case ext_ConnectMode.ext_cm_Startup:
        case ext_ConnectMode.ext_cm_AfterStartup:

            if (_applicationObject.ActiveDocument != null)
            {
                MessageBox.Show(_applicationObject.ActiveDocument.ActiveWindow.Caption);
            }
            break;
    }
}
  • Run the add-in project and in the new Visual Studio instance create a Windows Form application project.
  • A project is created and the window "Form1.cs [Design]" is active.
  • Double-click the form so that a new "Form1.cs" window becomes active (the one with the code)
  • Set the focus on the Solution Explorer toolwindow.
  • Go to "Tools", "Add-In Manager", and load the add-in.
  • The add-in shows "Form1.cs [Design]" as the active window of the active document, instead of "Form1.cs". If a toolwindow (such as the Solution Explorer) doesn't have the focus, the add-in shows "Form1.cs" as expected.


Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)


Top