Ver versión en español    
 
Home
10 Reasons to use MZ-Tools
MZ-Tools 6.0 for VS.NET
Editions
Features
Online Documentation
MZ-Tools SDK
Download
Purchase
Version History (RSS)  
FAQ & Support
MZ-Tools 3.0 for VB6 & VBA
Features
Online Documentation
Download (freeware)
Donations (Paypal)
Version History (RSS)  
FAQ & Support
User Reviews
Community Place
Contact  
For Add-In Developers
About
   
 
User Testimonials

I'm an avid supporter of MZ-Tools. It's a product I couldn't do without and your level of support is outstanding.

Jan Hyde (Visual Basic MVP)

You will soon wonder how you ever lived without it.

Andy Maggs

More user reviews
 
HOWTO: Locate commandbars in international versions of Visual Studio

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

    There are a couple of problems with this approach:

    - Many commandbar popups are indexed in the DTE.CommandBars collection, so it would be much more simple to locate the "Tools" commandbar popup in the DTE.CommandBars collection using the English name, as we did for the "MenuBar" commandbar.

    - For commandbar popups that are not indexed in the DTE.CommandBars collection (such as the "View" commandbar), for which you really need to locate its CommandBarControl inside a Controls collection and then cast it to CommandBarPopup, the approach used by the wizard requires a CommandBar.resx resource file with some localized names. A more simple and robust approach is shown here, which gets the commandbar associated to a CommandBarControl using only English names:

    Dim objMenuBarCommandBar As CommandBar
    Dim objViewCommandBar 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 = "View" Then ' English name
    
             objViewCommandBar = 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


MZ-Tools 6.0 for Visual Studio .NET

You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:

Buy MZ-Tools Now Download MZ-Tools Demo

   Top