Addin Button - Icon AND text (Image and Caption)  how?

7676
13
04-07-2011 07:50 AM
MichaelRobb
Occasional Contributor III
Hi everyone,

Simple but frustrating problem:
Prior to arcAddinx, there was no problem choosing to have Image / Text or Both for any button when a toolbar was loaded using the following code below triggered off the ENABLED using ESRICommandStyles.  (IToolbarDef)

        Public Sub SetAsIconAndText()

                ' Make sure button is showing Icon And Text
                Dim pUID As New UID
                pUID.Value = "{F53B91E1-C520-41a7-941C-686AA071D1A2}"  ' Toolbar GUID

            Dim pCommandBar As ESRI.ArcGIS.Framework.ICommandBar
            pCommandBar = m_application.Document.CommandBars.Find(pUID, True, False)

            Dim i As Integer
            If Not pCommandBar Is Nothing Then
                For i = 0 To pCommandBar.Count - 1
                    If pCommandBar.Item(i).Name = "FocusCorp_FocusEllipse" Then ' use the Property NAME of the button
                        pCommandBar.Item(i).Style = esriCommandStyles.esriCommandStyleIconAndText
                        Exit For
                    End If
                Next
            End If


        End Sub


I am aware that a user can AFTER the fact customize, but why would ESRI have left this functionality out with Addin XML?

Is there anyone that has found the means to control what each button displays on created toolbars when using Addins as far as Image and Caption?

Suggestions appreciated,

vb.net

Thanks
0 Kudos
13 Replies
MichaelRobb
Occasional Contributor III
I'm not sure if it's completely right, but I've used this code inside the OnUpdate method and it works for me, I've no need to change the style manually.
Note that the OnUpdate method is called many times during the start up of ArcMap, but the first few .


No, OnUpdate is Not called when a NEW arcmap session is opened.  I tried several times  with a fresh Addin, then debug or run arc, there was no OnUpdate trigger, therefore, no adjustment to the command style.
OnUpdate was not triggered until you pressed the actual button, but a new Addin, toolbar shows up, and ONLY the icon shows up even with the code in Protected Overrides Sub OnUpdate().   OR if you were lucky to hit a framework OnUpdate run, used for example to check if an extension is enabled.

In debugging, was not stopped (break point) either when a fresh arc was opened meaning OnUpdate was never called on New Document.

There must be something you had done prior, or manually changed it at some point or just got lucky the framework called the OnUpdate.  Change your code to be esriCommandStyleTextOnly and see if its ONLY text (and see if it changes), then change it to a different enum again and see if the change follows.

No Overrides from a button perspective will work.
OnUpdate is random and multiple times called by the framework... If it manages to update the button, you hit a 'right time' scenario, and you would be running the code over and over.
When I set OnDemand = false in the esriaddinx, I cant even get Arc to load, it just crashes... i believe due to the command not initialized yet, and already trying to change the commands style.


The only proper method is to add an extension and run on NEW and OPEN document events, to control the command style as stated prior.
this ensures the commandstyle change WILL occur right at startup.

ref:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//001v000001qw000000
0 Kudos
MichaelRobb
Occasional Contributor III
The correct way:

Using an extension:

Here is the Class.vb called in the esriaddinx:

[HTML]
Imports System.Windows.Forms

Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Framework


Public Class YOUREXTENSIONClassName

    Inherits ESRI.ArcGIS.Desktop.AddIns.Extension
'Not needed for this example
  '  Private c_mainMenuID As String = "{1E739F59-E45F-11D1-9496-080009EEBECB}" ' Main menubar

    Public Sub New()

    End Sub

    Protected Overrides Sub OnStartup()

        WireDocumentEvents()

    End Sub

    Protected Overrides Sub OnShutdown()

    End Sub

    Private Sub WireDocumentEvents()

        AddHandler My.ArcMap.Events.NewDocument, AddressOf ArcMapNewDocument
        AddHandler My.ArcMap.Events.OpenDocument, AddressOf ArcMapOpenDocument

    End Sub

   Private Sub ArcMapNewDocument()

        Try

          SetAsIconAndText(Your UID OR ClassID value, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleTextOnly )

        Catch ex As Exception
            MessageBox.Show(ex.Message & vbNewLine & "Details: " & ex.StackTrace, "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

        End Try

    End Sub

  Private Sub ArcMapOpenDocument()

        Try

           SetAsIconAndText(Your UID OR ClassID value, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleTextOnly )

        Catch ex As Exception
            MessageBox.Show(ex.Message & vbNewLine & "Details: " & ex.StackTrace, "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

        End Try

    End Sub

  Public Sub SetAsIconAndText(ByVal UIDValue As String, ByVal CommandStyle As ESRI.ArcGIS.SystemUI.esriCommandStyles)


        'control text or Text and icon or icon only
        Dim pUID As New UID
        pUID.Value = UIDValue 'YOUR COMMAND BUTTON UID VALUE OR ButtonID name in esriaddinx file of that Addin.

        Dim pCommandItem As ESRI.ArcGIS.Framework.ICommandItem
        pCommandItem = My.ArcMap.Application.Document.CommandBars.Find(pUID, True, False)

        pCommandItem.Style = CommandStyle



    End Sub
End Class

[/HTML]
0 Kudos
DubravkoAntonic
New Contributor III
Didn't try for ArcMap 10 but in prior versions 9.3.1 and earlier ,for each button, properties Enabled and Checked was called on regular basis, like once a second, if that can help you.

regards Dubravko
0 Kudos
SoniaScialanca
New Contributor
My personal experience: I also tried with the extension and when I put the code inside that class the button object was not completely loaded and the result was that I got a button with only the icon, the first time the extension was initialized.
I had to uncheck/check the extension to make that code work and to get the style changed.
This is why I moved the code into the OnUpdate method and when I debug I can see this method called, I don't now why my and your addin work different... 😞
0 Kudos