Logo
HOWTO: Select nodes in the Solution Explorer from a Visual Studio add-in

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

This article provides a sample to select a node in the Solution Explorer of Visual Studio from an add-in.

More Information

The Solution Explorer is a toolwindow whose inner content can be retrieved using EnvDTE80.DTE2.ToolWindows.SolutionExplorer. This property returns an object of the type EnvDTE.UIHierarchy, which has a UIHierarchyItems property which would allow to iterate recursively the nodes of the hierarchy. Each EnvDTE.UIHierarchyItem node, apart from a UIHierarchyItems property to get its children, has properties such as Name (to identify the node), Object (to get the actual node type, such as EnvDTE.Project, EnvDTE.ProjectItem, etc.), IsSelected (to get if the node is selected) and a Select method that receives the selection kind (vsUISelectionTypeSelect for single selection, vsUISelectionTypeExtend to include the node in the current selection, etc.).

However, the EnvDTE.UIHierarchy type has a GetItem helper method that returns an EnvDTE.UIHierarchyItem given its full path in the Solution Explorer (solution\project\etc.)

The following add-in, when loaded, selects the "System" node of the "References" node of the first project of the currently loaded solution:

Language: C#   Copy Code Copy Code (IE only)
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;

namespace MyAddin1
{
   public class Connect : IDTExtensibility2
   {
      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_AfterStartup:

               // The add-in has been loaded after Visual Studio was loaded. Since Visual Studio
               // is fully initialized, we can initialize the add-in
               InitializeAddIn();
               break;

            case ext_ConnectMode.ext_cm_Startup:

               // The add-in has been loaded with Visual Studio. Do nothing until Visual Studio 
               // is fully initialized (the OnStartupComplete method will be called)
               break;

            case ext_ConnectMode.ext_cm_UISetup:

               // Do nothing in this case
               break;
         }
      }

      public void OnStartupComplete(ref Array custom)
      {
         InitializeAddIn();
      }

      private void InitializeAddIn()
      {
         string solutionName;
         string projectName;

         try
         {
            solutionName = _applicationObject.Solution.Properties.Item("Name").Value.ToString();
            projectName = _applicationObject.Solution.Projects.Item(1).Name;

            SelectSolutionExplorerNode(solutionName + "\\" + projectName + "\\References\\System");
         }
         catch (Exception ex)
         {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
         }
      }

      private void SelectSolutionExplorerNode(string nodePath)
      {
         EnvDTE.UIHierarchyItem item;

         try
         {
            item = _applicationObject.ToolWindows.SolutionExplorer.GetItem(nodePath);

            item.Select(vsUISelectionType.vsUISelectionTypeSelect);
         }
         catch (Exception ex)
         {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
         }
      }

      public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
      {
      }

      public void OnAddInsUpdate(ref Array custom)
      {
      }

      public void OnBeginShutdown(ref Array custom)
      {
      }
   }
}
Language: VB.NET   Copy Code Copy Code (IE only)
Imports System
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80

Public Class Connect
   Implements IDTExtensibility2

   Private _applicationObject As DTE2
   Private _addInInstance As AddIn

   Public 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, EnvDTE80.DTE2)
      _addInInstance = CType(addInInst, EnvDTE.AddIn)

      Select Case connectMode

         Case ext_ConnectMode.ext_cm_AfterStartup

            ' The add-in has been loaded after Visual Studio was loaded. Since Visual Studio
            ' is fully initialized, we can initialize the add-in
            InitializeAddIn()

         Case ext_ConnectMode.ext_cm_Startup

            ' The add-in has been loaded with Visual Studio. Do nothing until Visual Studio 
            ' is fully initialized (the OnStartupComplete method will be called)

         Case ext_ConnectMode.ext_cm_UISetup

            ' Do nothing in this case

      End Select

   End Sub

   Public Sub OnStartupComplete(ByRef custom As Array) Implements IDTExtensibility2.OnStartupComplete

      InitializeAddIn()

   End Sub

   Private Sub InitializeAddIn()

      Dim solutionName As String
      Dim projectName As String

      Try

         solutionName = _applicationObject.Solution.Properties.Item("Name").Value.ToString()
         projectName = _applicationObject.Solution.Projects.Item(1).Name

         SelectSolutionExplorerNode(solutionName & "\" & projectName & "\References\System")

      Catch ex As Exception

         System.Windows.Forms.MessageBox.Show(ex.ToString())

      End Try

   End Sub

   Private Sub SelectSolutionExplorerNode(ByVal nodePath As String)

      Dim item As EnvDTE.UIHierarchyItem

      Try

         item = _applicationObject.ToolWindows.SolutionExplorer.GetItem(nodePath)

         item.Select(vsUISelectionType.vsUISelectionTypeSelect)

      Catch ex As Exception

         System.Windows.Forms.MessageBox.Show(ex.ToString())

      End Try

   End Sub

   Public Sub OnDisconnection(ByVal disconnectMode As ext_DisconnectMode, ByRef custom As Array) _
      Implements IDTExtensibility2.OnDisconnection

   End Sub

   Public Sub OnAddInsUpdate(ByRef custom As Array) Implements IDTExtensibility2.OnAddInsUpdate
   End Sub

   Public Sub OnBeginShutdown(ByRef custom As Array) Implements IDTExtensibility2.OnBeginShutdown
   End Sub

End Class

Related articles



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


Top