| Author: |
Carlos J. Quintero (Microsoft MVP) |
Applies to: |
Microsoft Visual Studio .NET 2002 |
| Date: |
January 2007 |
|
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
The CommandBar class has two properties, Name and NameLocal, that expose its
English name and the localized name. The CommandBarControl class has a Caption
that is always localized but the CommandBar.Controls collection does not allow
you to locate a CommandBarControl by its (unlocalized) English Name to get its
associated CommandBarPopup (if any).
This article explains how to locate commandbars using only the English name
in international versions of Visual Studio which can have localized names.
More Information
There are two cases in which you may want to locate a commandbar by name:
- You want to locate a commandbar from the DTE.CommandBars collection. In
this case you can use the English name when calling DTE.CommandBars.Item(commandbarEnglishName) to locate the commandbar since
that method works in all international versions of Visual Studio. When you
create a VB.NET add-in for Visual Studio 2005 using the wizard and you
specify that you want an item under the "Tools" menu, notice that the wizard
creates this code correctly using "MenuBar" instead of a localized name
(such as "Barra de menús" in Spanish):
Dim commandBars As CommandBars = CType(_applicationObject.CommandBars, CommandBars)
Dim menuBarCommandBar As CommandBar = commandBars.Item("MenuBar")
- You want to locate a commandbar popup, such as the "Tools" menu. The code that the wizard
generates in this case is the following:
Dim toolsMenuName As String
Try
Dim resourceManager As System.Resources.ResourceManager = _
New System.Resources.ResourceManager("MyAddin.CommandBar", _
System.Reflection.Assembly.GetExecutingAssembly())
Dim cultureInfo As System.Globalization.CultureInfo = _
New System.Globalization.CultureInfo(_applicationObject.LocaleID)
toolsMenuName = resourceManager.GetString(String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools"))
Catch e As Exception
toolsMenuName = "Tools"
End Try
Dim commandBars As CommandBars = CType(_applicationObject.CommandBars, CommandBars)
Dim menuBarCommandBar As CommandBar = commandBars.Item("MenuBar")
Dim toolsControl As CommandBarControl = menuBarCommandBar.Controls.Item(toolsMenuName)
Dim toolsPopup As CommandBarPopup = CType(toolsControl, CommandBarPopup)
The approach used by the wizard requires a
CommandBar.resx resource file with localized names for every Visual Studio
international version. A more simple and
robust approach is shown here, which gets the commandbar associated to a
CommandBarControl using only English names, it doesn't require the CommandBar.resx
file and it works in all international versions of Visual Studio:
Dim objMenuBarCommandBar As CommandBar
Dim objToolsCommandBar As CommandBar
Dim objCommandBarControl As CommandBarControl
Dim objCommandBarPopup As CommandBarPopup
objMenuBarCommandBar = _applicationObject.CommandBars.Item("MenuBar")
For Each objCommandBarControl In objMenuBarCommandBar.Controls
If objCommandBarControl.Type = MsoControlType.msoControlPopup Then
objCommandBarPopup = DirectCast(objCommandBarControl, CommandBarPopup)
If objCommandBarPopup.CommandBar.Name = "Tools" Then ' English name
objToolsCommandBar = objCommandBarPopup.CommandBar
Exit For
End If
End If
Next
Related articles
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
|