Logo
BUG: TextChanges CommandFlag not honored for toolbars in Visual Studio packages

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2010
Date: September 2014   Microsoft Visual Studio 2012
      Microsoft Visual Studio 2013
Introduction

The "TextChanges" CommandFlag used in the .vsct file of packages allows you to change the text of the UI item at run-time in the BeforeQueryStatus event handler of a MenuCommand. While this event handler is called for buttons and menu entries, it is not called for toolbars.

More Information

The following package reproduces the problem. It declares in the .vsct file a toolbar, a main menu and a command that is placed on both. Both the toolbar and the main menu are set as "TextChanges" (and "DynamicVisibility"):

Language: XML   Copy Code Copy Code (IE only)
<Menus>
   <Menu guid="guidVSPackage1CmdSet" id="cmdidMyToolbar" type="Toolbar" >
      <Parent guid="guidVSPackage1CmdSet" id="cmdidMyToolbar"/>
      <CommandFlag>DefaultDocked</CommandFlag>
      <CommandFlag>DynamicVisibility</CommandFlag>
      <CommandFlag>TextChanges</CommandFlag>
      <Strings>
         <ButtonText>My Toolbar (ButtonText)</ButtonText>
         <MenuText>My Toolbar (MenuText)</MenuText>
         <CanonicalName>My Toolbar (CanonicalName)</CanonicalName>
         <LocCanonicalName>My Toolbar (LocCanonicalName)</LocCanonicalName>
         <CommandName>My Toolbar (CommandName)</CommandName>
      </Strings>
   </Menu>

   <Menu guid="guidVSPackage1CmdSet" id="cmdidMyMenu" type="Menu" >
      <Parent guid="guidSHLMainMenu" id="IDG_VS_MM_TOOLSADDINS"/>
      <CommandFlag>DynamicVisibility</CommandFlag>
      <CommandFlag>TextChanges</CommandFlag>
      <Strings>
         <ButtonText>My Menu (ButtonText)</ButtonText>
         <MenuText>My Menu (MenuText)</MenuText>
         <CanonicalName>My Menu (CanonicalName)</CanonicalName>
         <LocCanonicalName>My Menu (LocCanonicalName)</LocCanonicalName>
         <CommandName>My Menu (CommandName)</CommandName>
      </Strings>
   </Menu>
</Menus>
In the code, declare two event handlers for the BeforeQueryStatus event to change the text when called:

Language: C#   Copy Code Copy Code (IE only)
protected override void Initialize()
{
   CommandID commandID;
   OleMenuCommand oleMenuCommand; 
   OleMenuCommandService oleMenuCommandService;
      
   base.Initialize();
      
   oleMenuCommandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
   if (oleMenuCommandService != null)
   {
      commandID = new CommandID(GuidList.guidVSPackage1CmdSet, PkgCmdIDList.cmdidMyToolbar);
      oleMenuCommand = new OleMenuCommand(null, commandID);
      oleMenuCommand.BeforeQueryStatus += oleMenuCommand_BeforeQueryStatus;
      oleMenuCommandService.AddCommand(oleMenuCommand);
      
      commandID = new CommandID(GuidList.guidVSPackage1CmdSet, PkgCmdIDList.cmdidMyMenu);
      oleMenuCommand = new OleMenuCommand(null, commandID);
      oleMenuCommand.BeforeQueryStatus += oleMenuCommand_BeforeQueryStatus;
      oleMenuCommandService.AddCommand(oleMenuCommand);
   }
}
      
private void oleMenuCommand_BeforeQueryStatus(object sender, EventArgs e)
{
   CommandID commandId;
   OleMenuCommand oleMenuCommand;
      
   oleMenuCommand = sender as OleMenuCommand;
      
   if (oleMenuCommand != null)
   {
      commandId = oleMenuCommand.CommandID;
      
      if (commandId != null)
      {
         switch (commandId.ID)
         {
            case PkgCmdIDList.cmdidMyToolbar:
      
               oleMenuCommand.Text = "My Toolbar changed";
               break;
      
            case PkgCmdIDList.cmdidMyMenu:
      
               oleMenuCommand.Text = "My Menu changed";
               break;
         }
      }
   }
}


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