Hide  Button on Addin Toolbar (ArcGIS 10.0)

4372
11
Jump to solution
06-25-2013 01:52 AM
TereseRowekamp
New Contributor III
I'd like to hide some buttons on an addin toolbar. It appears that isn't possible - there are only Enable or Checked properties.

First question - am I missing an easy way to hide an existing button on a toolbar?

Second question - if there is no way to hide a button, does that mean I need to add/remove the button to the toolbar programatically? Can someone point me to a code sample to do this? My buttons need to be in a particular order, and the ones I need to hide/show are in the middle - is there a way to add a button in a particular location?

Thanks for any help - I've been searching for code samples and failing to find examples of what I'm trying to do.

update:
- it occurs to me now that I only need to know how to remove a button. The buttons that should be shown depend on the particular user's level of access, and I know what that is when my addin initialization code is being run. So I can create the toolbar with all the buttons, and then just remove ones that shouldn't be there if the user shouldn't have access to that option.

So I'm just looking for a code sample to show how to remove a button from a toolbar.
0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor
Terese,

When you declared the pointer m_appStatusEvents did you declare it in the manner of:

Private m_mapStatusEvents As IApplicationStatusEvents_Event

For some reason an Event interface in VB .net needs to have that "_Event" at the end of it. No idea why but thats what must be done.

Duncan

View solution in original post

0 Kudos
11 Replies
DuncanHornby
MVP Notable Contributor
Teresa,

I've not tried this with an AddIn button, so you'll need to test this but you could get a handle on the toolbar then using the interface ICommandBar get a handle on the button as an ICommandItem. This has a Delete method to remove it from the toolbar

Duncan
0 Kudos
TereseRowekamp
New Contributor III
Thanks for the reply Duncan - I've been so busy workin on other parts of this project that I didn't see your reply until just now.

I'm trying to work with ICommandBar to find and remove a button, but additional questions come up:

1. Is there a way within the button code itself to reference that button (something like Me) or do I have to find the commandbar and then the command (just looking for a shortcut).

2. Where do I run this code from? Ideally I just want the toolbar to appear without those buttons so the user doesn't know any better about what's missing. I've got other initialization code I've put into the New sub of one of the buttons on the toolbar. I tried to add the following code to the New sub on that button

            Dim commandBars As ESRI.ArcGIS.Framework.ICommandBars = clsGlobals.app.Document.CommandBars
            Dim barID As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass
            barID.Value = "Test_Toolbar"
            Dim barItem As ESRI.ArcGIS.Framework.ICommandItem = commandBars.Find(barID, False, False)

At runtime, when the code hits the last line, it generates this error:
Catastrophic failure (Exception from HRESULT:0x8000FFFF (E_UNEXPECTED))

So, I guess this isn't the way to go!

Is the toolbar created at this point? How do I access something like an addin AfterLoad type of event?
0 Kudos
TereseRowekamp
New Contributor III
OK, I just did another test.
I created another button on the toolbar and put the following code in OnClick:

        Try
            Dim commandBars As ESRI.ArcGIS.Framework.ICommandBars = clsGlobals.app.Document.CommandBars
            Dim barID As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass
            barID.Value = "Test_Toolbar"
            Dim barItem As ESRI.ArcGIS.Framework.ICommandItem = commandBars.Find(barID, False, False)

            If (barItem Is Nothing) OrElse barItem.Type <> ESRI.ArcGIS.Framework.esriCommandTypes.esriCmdTypeToolbar Then Exit Sub

            Dim commandBar As ESRI.ArcGIS.Framework.ICommandBar = CType(barItem, ESRI.ArcGIS.Framework.ICommandBar)
            Dim commandID As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass
            commandID.Value = "Test_Button"
            If Not commandBar.Find(commandID, False) Is Nothing Then
                MsgBox("button found")
                Dim cmd As ESRI.ArcGIS.Framework.ICommandItem = commandBar.Find(commandID, False)
                cmd.Delete()
            End If


        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try

When I click the new button on the toolbar, the button referenced in the code disappears.
So the question is, where do I run this code from so I don't have to click a button to get the other button to delete?
0 Kudos
TereseRowekamp
New Contributor III
One more update. I kind of have it working.

I added an extension to my addin and set the OnStartup code as follows:

AddHandler My.ArcMap.Events.OpenDocument, AddressOf OpenOrNewDocument
AddHandler My.ArcMap.Events.NewDocument, AddressOf OpenOrNewDocument

The OpenOrNewDocument Sub is as follows:

    Private Sub OpenOrNewDocument()

        'hide buttons/tools for readonly user
        If clsGlobals.intUserRole > 1 Then Exit Sub 'not read only user

        Dim UID As New ESRI.ArcGIS.esriSystem.UID
        Dim commandItem As ESRI.ArcGIS.Framework.ICommandItem

        UID.Value = My.ThisAddIn.IDs.btnPushToGeo
        commandItem = My.ArcMap.Application.Document.CommandBars.Find(UID, False, False)
        commandItem.Delete()

        UID.Value = My.ThisAddIn.IDs.tolCreateNew
        commandItem = My.ArcMap.Application.Document.CommandBars.Find(UID, False, False)
        commandItem.Delete()

    End Sub

I tried putting the code to hide the buttons in the OnStart routine of the extension, but ArcMap crashes with no error messages reported. This way my code is triggered when the user opens a map or goes to a new map. The first time the toolbar is turned on, all the buttons are visible (but I have them disabled).

If the user then opens an mxd, goes to a new one or closes and re-opens ArcMap (without closing the toolbar), the buttons/tools that should be hidden are hidden and stay hidden unless the user closes the toolbar and re-opens it.

If anyone reading this can tell me where to better implement this code so that it is executed when the toolbar is created, I would appreciate it.
0 Kudos
DuncanHornby
MVP Notable Contributor
OK dumb question coming up, why don't you just make the toolbar without the buttons? If the intention is to remove them as soon as the document opens why not simply create the toolbar without them?
0 Kudos
TereseRowekamp
New Contributor III
Because we have read-only users and higher-levell users. The read-only users don't get the buttons, the other users do.

Note the line of code:

Private Sub OpenOrNewDocument()

'hide buttons/tools for readonly user
If clsGlobals.intUserRole > 1 Then Exit Sub 'not read only user
0 Kudos
DuncanHornby
MVP Notable Contributor
Terese,

Have a look at this sample code it shows the use of IApplicationStatus interface to test if the framework has been initialized first before making changes to the GUI.

Duncan
0 Kudos
TereseRowekamp
New Contributor III
Thanks for continuing to try to figure this out, Duncan.

I looked at the code at the link you included but got stuck when I couldn't get the Initialized event setup correctly using the following:

        m_appStatusEvents = TryCast(My.ThisApplication, IApplicationStatusEvents)
        AddHandler m_appStatusEvents.Initialized, AddressOf OnInitialized

I get an error that 'Initialized is not an event of ESRI.ArcGIS.Framework.IApplicationStatusEvents', even though when I use the Object Browser to look at IApplicationStatusEvents, it clearly lists Initialized.

At this point, I need to get something to the client and they are OK with it working the way it is, which isn't perfect but does work once the user opens a map file.

Thanks for all your help.

Terese
0 Kudos
DuncanHornby
MVP Notable Contributor
Terese,

When you declared the pointer m_appStatusEvents did you declare it in the manner of:

Private m_mapStatusEvents As IApplicationStatusEvents_Event

For some reason an Event interface in VB .net needs to have that "_Event" at the end of it. No idea why but thats what must be done.

Duncan
0 Kudos