Export (0) Print
Expand All

How to: Manipulate Tree Views by Using UIHierarchy

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see How to: Convert an Add-in to a VSPackage.

Some tool windows in Visual Studio, such as the Solution Explorer, do not have explicit automation objects that you can use to manipulate their contents. These tool windows do, however, have a tree view — that is, a hierarchical, outline-style, node view — that you can programmatically access. The UIHierarchy object represents the tree views in these tool windows and enables you to iterate through them and view the contents of their nodes.

Object Name


UIHierarchy object

Represents the tree view in the specified tool window.

UIHierarchyItems collection

Represents all nodes in the tree view.

UIHierarchyItem object

Represents a single node in the tree view.

By using these objects and collections, you can:

  • Select (singly or multiply) and view nodes in the tree view.

  • Move the insertion point up or down in the tree view.

  • Return the value of the selected item or have it perform its default action.

The ToolWindows object (which is also returned from ToolWindows) allows easier referencing of the various tool windows in Visual Studio. For example, rather than using _applicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindOutput), you can now use _applicationObject.ToolWindows.OutputWindow.

Note Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Customizing Development Settings.

Although the UIHierarchy object represents the contents of nearly any tool window that has a tree view, such as Solution Explorer, the tool window itself is still a Window object. The UIHierarchyItems property returns the collection of top-level nodes in the specified tool window. In Solution Explorer, there is only a single top-level node (the solution). Consequently, the project node for this particular window is in the top-level node's collection rather than in the window's UIHierarchyItems collection.

Bearing this in mind, there are two ways to access a particular node (UIHierarchyItem) in a tree view:

  • By using the GetItem method to directly reference the desired node using a solution/project/item pattern.

  • By using UIHierarchyItems.Item.UIHierarchyItems... (a collection/item/collection pattern).

    To navigate deeper into a node nesting, just keep using this pattern. For example, to go to a node subordinate to the top-level node, you would use UIHierarchy.UIHierarchyItems.Item(1).UIHierarchyItems.Item(2).

Examples of how to use both techniques to access a lower-level node are demonstrated in the examples below.

These add-in examples demonstrate how to reference and use the various members of the UIHierarchy automation model to list all of the items in Solution Explorer.

The first example uses the GetItem method strategy of accessing the contents of the References node in Solution Explorer. For more information about how to run the add-in code, see How to: Compile and Run the Automation Object Model Code Examples.

Note Note

The example for Solution Explorer sends its data to a message box.

using System.Text;

public void OnConnection(object application, ext_ConnectMode _
  connectMode, object addInInst, ref Array custom)
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

public void listSlnExpNodes(DTE2 dte)
    // Requires reference to System.Text for StringBuilder.
    UIHierarchy UIH = dte.ToolWindows.SolutionExplorer;
    // Set a reference to the first level nodes in Solution Explorer. 
    // Automation collections are one-based.
    UIHierarchyItem UIHItem = 
    StringBuilder sb = new StringBuilder();

   // Iterate through first level nodes.
   foreach ( UIHierarchyItem file in UIHItem.UIHierarchyItems )
       // Iterate through second level nodes (if they exist).
       foreach ( UIHierarchyItem subitem in file.UIHierarchyItems )
           sb.AppendLine("   "+subitem.Name);

The following example illustrates how to use UIHierarchy to list the contents of the tree view of the Solution Explorer window.

Sub cvTreeView()
    Dim uih As UIHierarchy = DTE.ToolWindows.SolutionExplorer
    Dim uihItem As UIHierarchyItem
    Dim uihItems As UIHierarchyItems = uih.UIHierarchyItems
    Dim msg As String
    For Each uihItem In uihItems
        msg += uihItem.Name & vbCr
End Sub
© 2014 Microsoft