title: VSPackage: Modify toolbar button text/tooltip at runtime
body: <p>I have been developing a Visual Studio extension as an Add-in for VS2008/2010. The new version now is to be done as a VSIX package for VS2010 only (as it has to be .NET 4 anyway), and I am having some troubles with (rather simple, I would think) UI handling.</p>
  
  <p>The extension primarily consists of a toolbar with some buttons that launch various actions, forms etc., and a few that are only used as labels to display some state information. The "label" buttons themselves just show very short and concise information, while the tooltips provide more detail.</p>
  
  <p>While I didn't think the whole Add-in thing was very elegant, doing this kind of stuff was pretty straightforward (though my approach may have been a little amateurish). When adding the commands to the toolbar, I would "save" the "label" buttons in specific local variables, allowing me to later set the caption and tooltip text at will.</p>
  
  <p>In a VSPackage, the equivalent to <em>Microsoft.VisualStudio.CommandBars.CommandBarButton</em> appears to be <em>OleMenuCommand</em>. Finding the "label" command through the <em>MenuCommandService</em> is no problem, modifying it as needed is, however.</p>
  
  <p>For the purpose of finding out how to do this, I just have a toolbar with two buttons in a group. <em>btnAction</em> is very simple; just an icon and an execute handler to change the text on the other button, no CommandFlags.
  <em>btnLabel</em> looks like this in the .vsct:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Button guid="guidVSPackageBuilderTutorialCommandSet" id="btnLabel" priority="0x0100"&gt;
      &lt;CommandFlag&gt;DefaultDisabled&lt;/CommandFlag&gt;
      &lt;CommandFlag&gt;DontCache&lt;/CommandFlag&gt;
      &lt;CommandFlag&gt;NoCustomize&lt;/CommandFlag&gt;
      &lt;CommandFlag&gt;TextChanges&lt;/CommandFlag&gt;
      &lt;CommandFlag&gt;TextOnly&lt;/CommandFlag&gt;
      &lt;Strings&gt;
          &lt;CommandName&gt;cmdidbtnLabel&lt;/CommandName&gt;
          &lt;ButtonText&gt;btnLabel&lt;/ButtonText&gt;
          &lt;MenuText&gt;btnLabel&lt;/MenuText&gt;
          &lt;ToolTipText&gt;Tooltip btnLabel&lt;/ToolTipText&gt;
      &lt;/Strings&gt;
  &lt;/Button&gt;
  </code></pre>
  
  <p>The first problem with this is that when I use <strong>TextChanges</strong>, the <em>ToolTipText</em> string is ignored and the <em>ButtonText</em> is initially used for the tooltip as well.</p>
  
  <p>The handler code for the action button is as follows:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private int iClickCount = 0;
  
  protected override void btnActionExecuteHandler(object sender, EventArgs e)
  {
      var svc = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
  
      CommandID idBtnLabel = new CommandID(GuidList.guidVSPackageBuilderTutorialCmdSet, (int)PkgCmdIDList.btnLabel);
      var cmd = svc.FindCommand(idBtnLabel) as OleMenuCommand;
  
      cmd.Text = "Clicked " + (++iClickCount) + " times";
  }
  </code></pre>
  
  <p>This changes the caption of <em>btnLabel</em> as expected, but as there is no way of explicitly specifying the tooltip (the <em>OleMenuCommand</em> object just has a <em>Text</em> property, unlike <em>CommandBarButton</em>, which has both <em>Caption</em> and <em>TooltipText</em>), the tooltip is always set to the same string as the caption. From what I understand, this is because with FindCommand() I am not actually getting the UI button, but only the underlying command, which doesn't care about tooltips.</p>
  
  <p>What's even more confusing is what happens when I use the <strong>TextChangesButton</strong> CommandFlag instead of <strong>TextChanges</strong>. The button will now correctly display the tooltip text defined in the .vsct, but neither the caption nor the tooltip will change when I click the other button - though when I check the <em>btnLabel</em> command's <em>Text</em> property, it is set to what I expect ("Clicked x times"). Does <strong>TextChangesButton</strong> kind of "decouple" the properties of the command and the button ? If so, this is pretty much what I want (I don't care about the command, as there is nothing to execute anyway; <em>btnLabel</em> will always be disabled), but how can I access the button and its particular string properties ?</p>
  
  <p>I looked through the various IVs interfaces and SVs services but couldn't find something appropriate, and the documentation (and IntelliSense help) doesn't seem to be very extensive.</p>