Logo
HOWTO: Guessing the name of a command bar to add a custom menu entry in Visual Studio .NET add-ins

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: July 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

When you want to add a custom menu entry to a built-in command bar of Visual Studio .NET, typically to a context menu in the code editor or solution explorer, the first step is to guess the name of that command bar to get it through DTE.CommandBars.Item(commandBarName). Since there are more than 200 command bars, some of them with non-intuitive names, this can be time-consuming. This article provides a tip to ease this task.

More Information

You can call the function below from your add-in code. The function receives as parameters the DTE object and the caption of a control that you know it belongs to the searched command bar. You should use a control caption as rare as possible. For example, if you want to know the name of the command bar that provides the context menu of a setup project in the solution explorer, you could use "&Install", since it is quite unique to that context menu. The candidate command bar names are shown in the Output window, along with the control captions on each candidate command bar to facilitate the identification if there is more than one.

Language: VB.NET   Copy Code Copy Code (IE only)
Sub GetCommandBarNameByControlCaption(ByVal objDTE As DTE, ByVal sControlCaption As String)
 
   Dim objCommandBar As CommandBar
   Dim objCommandBarControl1 As CommandBarControl
   Dim objCommandBarControl2 As CommandBarControl
   Dim colCommandBars As CommandBars
 
   Try

      ' The following cast is required in VS 2005 and higher because its DTE.CommandBars returns the type Object
      ' (because VS 2005 and higher uses for commandbars the type Microsoft.VisualStudio.CommandBars.CommandBars 
      ' of the new Microsoft.VisualStudio.CommandBars.dll assembly while VS.NET 2002/2003 used the 
      ' type Microsoft.Office.Core.CommandBars of the Office.dll assembly)

      colCommandBars = CType(objDTE.CommandBars, CommandBars)
 
      For Each objCommandBar In colCommandBars

         For Each objCommandBarControl1 In objCommandBar.Controls

            If objCommandBarControl1.Caption = sControlCaption Then
 
               Debug.WriteLine("----------------------------------------")
               Debug.WriteLine("Candidate CommandBar Name: " & """" & objCommandBar.Name & """")
               Debug.WriteLine("Captions on this command bar:")

               For Each objCommandBarControl2 In objCommandBar.Controls
                  Debug.WriteLine("  " & objCommandBarControl2.Caption)
               Next
 
               Exit For

            End If

         Next

      Next
 
   Catch objException As Exception

      MessageBox.Show(objException.ToString)

   End Try

End Sub
Language: C#   Copy Code Copy Code (IE only)
public void GetCommandBarNameByControlCaption(DTE dte, string controlCaption)
{
   CommandBars commandBars;

   try
   {

      // The following cast is required in VS 2005 and higher because its DTE.CommandBars returns the type Object
      // (because VS 2005 and higher uses for commandbars the type Microsoft.VisualStudio.CommandBars.CommandBars 
      // of the new Microsoft.VisualStudio.CommandBars.dll assembly while VS.NET 2002/2003 used the 
      // type Microsoft.Office.Core.CommandBars of the Office.dll assembly)

      commandBars = (CommandBars) dte.CommandBars;

      foreach (CommandBar commandBar in commandBars)
      {
         foreach (CommandBarControl commandBarControl1 in commandBar.Controls)
         {
            if (commandBarControl1.Caption == controlCaption)
            {
               Debug.WriteLine("----------------------------------------");
               Debug.WriteLine("Candidate CommandBar Name: " + "\"" + commandBar.Name + "\"");
               Debug.WriteLine("Captions on this command bar:");

               foreach (CommandBarControl commandBarControl2 in commandBar.Controls)
               {
                  Debug.WriteLine("  " + commandBarControl2.Caption);
               }

               break; 
            }
         }
      }
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.ToString());
   }
}

Related articles



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


Top