Button AddIn - programmatically changing the caption

3372
3
10-04-2011 10:30 AM
DirkVandervoort
Occasional Contributor II
Is it possible to change the caption of an AddIn button in code?

The button and it's containing toolbar are declared in the XML:
  <AddIn language="CLR" library="DeleteThisArcMapAddinProject.dll" namespace="DeleteThisArcMapAddinProject">
    <ArcMap>
      <Commands>
        <Button id="DeleteThisArcMapAddinProject_Button1" class="Button1" message="Yada yada." caption="My Button" tip="tooltip." category="Add-In Controls" />
     </Commands>
      <Extensions>
      </Extensions>
      <Toolbars>
        <Toolbar id="DeleteThisArcMapAddinProject_My_Toolbar" caption="My Toolbar" showInitially="true">
          <Items>
            <Button refID="DeleteThisArcMapAddinProject_Button1" />
          </Items>
        </Toolbar>
      </Toolbars>
    </ArcMap>
  </AddIn>


As a dumb example, I want to change the cation of the button in response to the OnClick event of the button. BITD I could do it in COM ArcObjects by creating an instance of an ICommandItem. Here's a failed try:
Protected Overrides Sub OnClick()
 Dim uid As UID = New UIDClass()
 uid.Value = "DeleteThisArcMapAddinProject_Button1"
 Dim mx As MxDocument = DirectCast(My.ArcMap.Application.Document, MxDocument)
 Dim cmdBars As ICommandBars = mx.CommandBars
 Dim button As ICommandItem = cmdBars.Find(uid, False, False)
 System.Windows.Forms.MessageBox.Show(button.Caption)
 button.Caption = "My New Caption"
 My.ArcMap.Application.CurrentTool = Nothing
End Sub


What's funny (sic) about this is that the button.Caption value is "My New Caption" for subsequent OnClick events, even though the caption in the UI stays "My Button".

  1. Can I change the caption in code?
  2. Or does the new declarative framework not allow me to do this?

TIA
0 Kudos
3 Replies
CarlQuirion
New Contributor

  1. Can I change the caption in code?
  2. Or does the new declarative framework not allow me to do this?



I am also interested in knowing the answer to this question and i am also interested in changing the tooltip and message properties.
My toolbar has a button that establish a connection to another system and i would like this button to read "Connect" when disconnected and "Disconnect" when connected and obviously, i would like to change their tooltips and message as well. Changing those two other properties result in exceptions.
(COMException : "This method cannot be called on built in commands.")

Looking forward to an answer to this.

Thanks
0 Kudos
JeffreyHamblin
New Contributor III
There seems to be a disconnect between the UI button caption and the ICommandItem.Caption property.

I tried some similar code in C#:
private void ChangeButtonCaption()
{
    // Get the collection of toolbars
    ICommandBars cmdBars = ArcMap.Application.Document.CommandBars;

    // Get the button
    UID uid = new UIDClass();
    uid.Value = ThisAddIn.IDs.TestButton;
    ICommandItem cmdItem = cmdBars.Find(uid, false, false);

    // skip out if no button
    if (cmdItem == null)
        return;

    // report the current caption
    System.Diagnostics.Debug.WriteLine("cmdItem.Caption: " + cmdItem.Caption);
    // change the caption
    cmdItem.Caption = "Test Button (clicked)";
    cmdItem.Refresh();
    // report the changed caption
    System.Diagnostics.Debug.WriteLine("cmdItem.Caption: " + cmdItem.Caption);

    // Cannot set Tooltip: get error "This method cannot be called on built in commands"
    // This is mentioned in documentation for this property.
    //cmdItem.Tooltip = "Test tooltip. (clicked)";
}


As the original poster reported, the UI caption never changes even though the caption property persists.

Further, after using ArcMap to change the UI caption (by opening the Customize dialog, then right-clicking on the button and editing the Name value), the new caption is reflected in the underlying ICommandItem.Caption property, but the UI caption still does not change when editing the property.

[added]
You can change the icon for the button. Inserting a line of code above with cmdItem.FaceID = 1 works. See the ICommandItem.FaceId documentation for more info.
0 Kudos
JeffreyHamblin
New Contributor III
According to another thread, the caption property bug will be fixed in 10.0 SP4 and 10.1:

Change Caption of Base Command
0 Kudos