How to: Control Solution Explorer

Solution Explorer is a tool window in the Visual Studio integrated development environment (IDE) that displays the contents of a solution, which includes the solution's projects and each project's items. Like other tool windows in Visual Studio, you can control its physical parameters, such as size, location, and whether it is docked or free-floating. For information about how to manipulate this tool window as well as other Visual Studio tool windows, see How to: Change Window Characteristics.

Solution Explorer does not have its own automation objects as such, but you can control the contents of its hierarchy to a certain extent by using UIHierarchy. To control projects and project items in the solution, use the project automation model. For more information, see Controlling Projects and Solutions.

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 ExportSettings on the Tools menu. For more information, see Visual Studio Settings.

To control Solution Explorer by using UIHierarchy

  1. If Solution Explorer is not already visible, click Solution Explorer on the View menu.

  2. Open a project that has a large number of elements, such as an Add-in project.

  3. In Solution Explorer, click a node that has at least two sub-nodes.

  4. Run the following code.

Example

This example demonstrates how to manipulate Solution Explorer by using UIHierarchy.

Imports System.Text
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE100Public Sub OnConnection(ByVal application As Object, ByVal _
  connectMode As ext_ConnectMode, ByVal addInInst As Object, _
  ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    ' Pass the applicationObject member variable to the code example.
    slnExplUIHierarchyExample(_applicationObject)
End Sub

Sub slnExplUIHierarchyExample(ByVal dte As DTE2)
    Dim UIH As UIHierarchy = dte.ToolWindows.SolutionExplorer
    ' Requires a reference to System.Text.
    ' Set a reference to the first level nodes in Solution Explorer. 
    ' Automation collections are one-based.
    Dim UIHItem As UIHierarchyItem = UIH.UIHierarchyItems.Item(1)
    Dim file As UIHierarchyItem
    Dim sb As New StringBuilder

    ' Iterate through first level nodes.
    For Each file In UIHItem.UIHierarchyItems
        sb.AppendLine(file.Name)
        ' Iterate through second level nodes (if they exist).
        Dim subitem As UIHierarchyItem
        For Each subitem In file.UIHierarchyItems
            sb.AppendLine("   " & subitem.Name)
            ' Iterate through third level nodes (if they exist).
            Dim subSubItem As UIHierarchyItem
            For Each subSubItem In subitem.UIHierarchyItems
                sb.AppendLine("        " & subSubItem.Name)
            Next
        Next
    Next
    MsgBox(sb.ToString)
 End Sub
using System.Text;
using EnvDTE;
using EnvDTE80;
using EnvDTE90;
using EnvDTE100;public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Pass the applicationObject member variable to the code example.
    slnExplUIHierarchyExample(_applicationObject);
}

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

    // Iterate through first level nodes.
    foreach ( UIHierarchyItem fid in UIHItem.UIHierarchyItems )
    {
        sb.AppendLine(fid.Name);
        // Iterate through second level nodes (if they exist).
        foreach ( UIHierarchyItem subitem in fid.UIHierarchyItems )
        {
            sb.AppendLine("   "+subitem.Name);
            // Iterate through third level nodes (if they exist).
            foreach ( UIHierarchyItem subSubItem in 
              subitem.UIHierarchyItems )
            {
                sb.AppendLine("        "+subSubItem.Name);
            }
        }
    }
    System.Windows.Forms.MessageBox.Show(sb.ToString());
}

See Also

Tasks

How to: Create an Add-In

Walkthrough: Creating a Wizard

Concepts

Introduction to the VSProject2 Object

Automation Object Model Chart

Other Resources

Creating and Controlling Environment Windows

Creating Add-ins and Wizards

Automation and Extensibility Reference