| Author: |
Carlos J. Quintero (Microsoft MVP) |
Applies to: |
Microsoft Visual Studio .NET 2002 |
| Date: |
July 2004 |
|
Microsoft Visual Studio .NET 2003 |
| |
|
|
Microsoft Visual Studio 2005 |
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 (VB.NET) 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.
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 because its DTE.CommandBars returns the type Object
' (because VS 2005 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
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#: 
|