Logo
HOWTO: Getting the 'View' command bar of Visual Studio .NET from an add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: August 2004   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
Introduction

This article describes how to get the "View" command bar of the main menu of Visual Studio .NET in order to add new entries to it from an add-in, since DTE.CommandBars.Item("View") returns a different command bar.

More Information

Command bar names are not unique in Visual Studio, so using the CommandBars.Item method may not return the expected commandbar if some name is duplicated. This is the case of the "View" command bar. In order to retrieve the "View" command bar of the main menu of Visual Studio .NET, you have to iterate through the controls of the "MenuBar" command bar, casting them to a command bar popup and checking its name. The following add-in shows the technique:

Language: VB.NET   Copy Code Copy Code (IE only)
Public Class Connect
   Implements Extensibility.IDTExtensibility2

   Private m_dte As EnvDTE.DTE
   Private m_addIn As EnvDTE.AddIn

   Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, _
      ByVal addInInst As Object, ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnConnection

      Try

         m_dte = CType(application, EnvDTE.DTE)
         m_addIn = CType(addInInst, EnvDTE.AddIn)

         Select Case connectMode

            Case ext_ConnectMode.ext_cm_Startup

               ' Do nothing yet, wait until the IDE is fully initialized (OnStartupComplete will be called)

            Case ext_ConnectMode.ext_cm_AfterStartup

               InitializeAddIn()

         End Select

      Catch objException As Exception
         MessageBox.Show(objException.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
      End Try

   End Sub

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

      InitializeAddIn()

   End Sub

   Private Sub InitializeAddIn()

      Dim commandBars As CommandBars
      Dim menuBarCommandBar As CommandBar
      Dim viewCommandBar As CommandBar
      Dim commandBarControl As CommandBarControl
      Dim commandBarPopup As CommandBarPopup
      Dim controlList As String

      Try

         commandBars = CType(m_dte.CommandBars, CommandBars)

         menuBarCommandBar = commandBars.Item("MenuBar")

         For Each commandBarControl In menuBarCommandBar.Controls

            If commandBarControl.Type = MsoControlType.msoControlPopup Then

               commandBarPopup = DirectCast(commandBarControl, CommandBarPopup)

               If commandBarPopup.CommandBar.Name = "View" Then

                  viewCommandBar = commandBarPopup.CommandBar

                  Exit For

               End If

            End If

         Next

         controlList = "Controls currently visible on the View CommandBar: " & _
            Microsoft.VisualBasic.ControlChars.CrLf

         For Each commandBarControl In viewCommandBar.Controls

            If commandBarControl.Visible Then

               controlList &= Microsoft.VisualBasic.ControlChars.CrLf & _
                  commandBarControl.Caption

            End If

         Next

         System.Windows.Forms.MessageBox.Show(controlList)

      Catch objException As System.Exception

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

      End Try

   End Sub

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

   End Sub

   Public Sub OnBeginShutdown(ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnBeginShutdown

   End Sub

   Public Sub OnAddInsUpdate(ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnAddInsUpdate

   End Sub

End Class
Language: C#   Copy Code Copy Code (IE only)
public class Connect: Extensibility.IDTExtensibility2
{
   private EnvDTE.DTE m_dte;
   private EnvDTE.AddIn m_addIn;

   public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, 
      object addInInst, ref System.Array custom)
   {
      try
      {
         m_dte = (EnvDTE.DTE) application;
         m_addIn = (EnvDTE.AddIn) addInInst;

         switch (connectMode)
         {
            case ext_ConnectMode.ext_cm_Startup:
               
               // Do nothing yet, wait until the IDE is fully initialized (OnStartupComplete will be called)
               break;

            case ext_ConnectMode.ext_cm_AfterStartup:
               
               InitializeAddIn();
               break;
         }
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
   }

   public void OnStartupComplete(ref System.Array custom)
   {
      InitializeAddIn();
   }
	
   private void InitializeAddIn()
   {
      CommandBars commandBars;
      CommandBar menuBarCommandBar = null;
      CommandBar viewCommandBar = null;
      CommandBarPopup commandBarPopup = null;
      string controlList = null;

      try 
      {

         commandBars = (CommandBars) m_dte.CommandBars;
         menuBarCommandBar = commandBars["MenuBar"];

         foreach (CommandBarControl commandBarControl in menuBarCommandBar.Controls) 
         {
            if (commandBarControl.Type == MsoControlType.msoControlPopup) 
            {
               commandBarPopup = (CommandBarPopup)commandBarControl;
     
               if (commandBarPopup.CommandBar.Name == "View") 
               {
                  viewCommandBar = commandBarPopup.CommandBar;
                  break; 
               }
            }
         }

         controlList = "Controls currently visible on the View CommandBar:\n";

         foreach (CommandBarControl commandBarControl in viewCommandBar.Controls) 
         {
            if (commandBarControl.Visible) 
            {
               controlList += "\n" + commandBarControl.Caption;
            }
         }

         System.Windows.Forms.MessageBox.Show(controlList);
      }
      catch (System.Exception objException) 
      {
         System.Windows.Forms.MessageBox.Show(objException.ToString());
      }

   }
	
   public void OnBeginShutdown(ref System.Array custom)
   {
   }

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

   public void OnAddInsUpdate(ref System.Array custom)
   {
   }

}

Related articles



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