Logo
HOWTO: Get the selected 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 get the name and kind of the selected nodes 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 in turn has a SelectedItems property. This other property, while typed as System.Object, is actually an array of EnvDTE.UIHierarchyItem, and for each EnvDTE.UIHierarchyItem its Object property can be an EnvDTE.Solution, EnvDTE.Project, EnvDTE.ProjectItem, etc.

The following add-in, when loaded, iterates the selected items in the Solution Explorer for 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()
      { 
         object[] selectedItems;

         try
         {

            selectedItems = (object[])_applicationObject.ToolWindows.SolutionExplorer.SelectedItems;

            foreach (EnvDTE.UIHierarchyItem selectedUIHierarchyItem in selectedItems)
            {
               if (selectedUIHierarchyItem.Object is EnvDTE.Project)
               {
                  System.Windows.Forms.MessageBox.Show("Project node is selected: " + selectedUIHierarchyItem.Name);
               }
               else if (selectedUIHierarchyItem.Object is EnvDTE.ProjectItem)
               {
                  System.Windows.Forms.MessageBox.Show("Project item node is selected: " + selectedUIHierarchyItem.Name);
               }
               else if (selectedUIHierarchyItem.Object is EnvDTE.Solution)
               {
                  System.Windows.Forms.MessageBox.Show("Solution node is selected: " + selectedUIHierarchyItem.Name);
               }
            }
         }
         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 selectedItems As Object()
      Dim selectedUIHierarchyItem As EnvDTE.UIHierarchyItem

      Try

         selectedItems = CType(_applicationObject.ToolWindows.SolutionExplorer.SelectedItems, Object())

         For Each selectedUIHierarchyItem In selectedItems

            If TypeOf selectedUIHierarchyItem.Object Is EnvDTE.Project Then

               System.Windows.Forms.MessageBox.Show("Project node is selected: " & selectedUIHierarchyItem.Name)

            ElseIf TypeOf selectedUIHierarchyItem.Object Is EnvDTE.ProjectItem Then

               System.Windows.Forms.MessageBox.Show("Project item node is selected: " & selectedUIHierarchyItem.Name)

            ElseIf TypeOf selectedUIHierarchyItem.Object Is EnvDTE.Solution Then

               System.Windows.Forms.MessageBox.Show("Solution node is selected: " & selectedUIHierarchyItem.Name)

            End If

         Next

      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 back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


Top