Logo
INFO: menuCommandBar.Controls["Tools"] works in international versions of Visual Studio 2010

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2010
Date: May 2011   Microsoft Visual Studio 2012
Updated: March 2013    
       
Introduction

This article discusses a new way of locating a commandbar popup in a parent commandbar in for Visual Studio 2010 that didn't work in international versions of Visual Studio before the 2010 version.

More Information

A Visual Studio commandbar contains controls (CommandBar.Controls collection) and each control is of the type CommandBarControl, which can be a button (CommandBarButton type), or a commandbar popup (CommandBarPopup type). The CommandBarPopup type has the CommandBar property to get its commandbar.

There are several methods to get a commandbar popup given its parent commandbar and its name, for example the "Tools" submenu of the main menu of Visual Studio:

Visual Studio 2010 offers another new approach: CommandBar.Controls[commandbarPopupName], for example: menuCommandBar.Controls["Tools"]. This approach only worked in English versions of Visual Studio 2005 and 2008, but not in international versions. However, in Visual Studio 2010 it works also in international versions.

The following add-in provides the sample code. To test it, install an international version of Visual Studio (you can do it on top of the English version) and go to the "Tools", "Options" window, "Environment", "International Settings" section to change the language of the Visual Studio user interface.

Language: C#   Copy Code Copy Code (IE only)
using System;
using Microsoft.VisualStudio.CommandBars;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;

public class MyAddIn : Extensibility.IDTExtensibility2
{
   private EnvDTE.DTE applicationObject;

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

         switch (connectMode)
         {
            case ext_ConnectMode.ext_cm_UISetup:

               // Do nothing for this add-in with temporary user interface
               break;

            case ext_ConnectMode.ext_cm_Startup:

               // The add-in was marked to load on startup
               // Do nothing at this point because the IDE may not be fully initialized
               // Visual Studio will call OnStartupComplete when fully initialized
               break;

            case ext_ConnectMode.ext_cm_AfterStartup:

               // The add-in was loaded by hand after startup using the Add-In Manager
               // Initialize it in the same way that when is loaded on startup
               AddTemporaryUI();
               break;
         }
      }
      catch (System.Exception e)
      {
         System.Windows.Forms.MessageBox.Show(e.ToString());
      }
   }

   public void OnStartupComplete(ref System.Array custom)
   {
      AddTemporaryUI();
   }

   public void AddTemporaryUI()
   {
      CommandBar menuCommandBar = null;
      CommandBarControl toolsCommandBarControl;
      CommandBarPopup toolsCommandBarPopup;
      CommandBar toolsCommandBar;
      CommandBars commandBars = null;
      
      try
      {
         commandBars = (CommandBars)applicationObject.CommandBars;

         menuCommandBar = commandBars["MenuBar"];

         // This didn't work in international versions of Visual Studio 2005 or 2008
         toolsCommandBarControl = menuCommandBar.Controls["Tools"];

         toolsCommandBarPopup = (CommandBarPopup) toolsCommandBarControl;

         toolsCommandBar = toolsCommandBarPopup.CommandBar;

         MessageBox.Show(toolsCommandBar.NameLocal);

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

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

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

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

}
Language: VB.NET   Copy Code Copy Code (IE only)
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports System.Windows.Forms

Public Class Connect
   Implements Extensibility.IDTExtensibility2

   Private applicationObject As EnvDTE.DTE

   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

         applicationObject = CType(application, EnvDTE.DTE)

         Select Case connectMode

            Case ext_ConnectMode.ext_cm_UISetup

               ' Do nothing for this add-in with temporary user interface

            Case ext_ConnectMode.ext_cm_Startup

               ' The add-in was marked to load on startup
               ' Do nothing at this point because the IDE may not be fully initialized
               ' Visual Studio will call OnStartupComplete when fully initialized

            Case ext_ConnectMode.ext_cm_AfterStartup

               ' The add-in was loaded by hand after startup using the Add-In Manager
               ' Initialize it in the same way that when is loaded on startup
               AddTemporaryUI()

         End Select

      Catch e As System.Exception
         System.Windows.Forms.MessageBox.Show(e.ToString)
      End Try

   End Sub

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

      AddTemporaryUI()

   End Sub

   Public Sub AddTemporaryUI()

      Dim menuCommandBar As CommandBar
      Dim toolsCommandBarControl As CommandBarControl
      Dim toolsCommandBarPopup As CommandBarPopup
      Dim toolsCommandBar As CommandBar
      Dim commandBars As CommandBars

      Try

         commandBars = DirectCast(applicationObject.CommandBars, CommandBars)

         menuCommandBar = commandBars.Item("MenuBar")

         '  This didn't work in international versions of Visual Studio 2005 or 2008
         toolsCommandBarControl = menuCommandBar.Controls.Item("Tools")

         toolsCommandBarPopup = DirectCast(toolsCommandBarControl, CommandBarPopup)

         toolsCommandBar = toolsCommandBarPopup.CommandBar

         MessageBox.Show(toolsCommandBar.NameLocal)

      Catch e As System.Exception
         System.Windows.Forms.MessageBox.Show(e.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

Related articles



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


Top