[GIS] How to find all ICommandItem instances of a custom button command in an ArcMap add-in

add-inarcgis-10.0arcobjectscux

I have an ArcMap 10.0 add-in in which I have a button that the user can place (using the Customize dialog in ArcMap) on any toolbar including user-created toolbars as well as standard ones or even 3rd party ones. They could even put multiple instances of this button on the same or many toolbars (what can I say, some people can be crazy).

I have some code I need to run on each ICommandItem instance (to set its FaceID property, see this question if interested) wrapping this button command.

How can I reliably find each of these instances?

Currently I only set the first (and only) one returned by ICommandBars.Find, leaving the rest alone. Not the worst thing in the world, but less than ideal.

I thought I had a lead with this snippet (List ArcMap Visible Toolbars) but this only lists standard ArcMap toolbars (those in the 'ESRI Mx Command Bars' component category in the registry). From that I could iterate over each command item in each toolbar.

This approach could work if I could get it to list all toolbars including user-defined ones (which are not in any category as far as I can tell), and, admittedly less-importantly, 3rd party ones (which may or may not always be in the same category used by the snippet, I'm not sure).

I found this crusty old unanswered post from 2003 on the ESRI forums asking how to find user toolbars: "Detect user defined toolbars programmatically" — don't you just hate dead ends?

Lastly I found an answer in this post, "looping through all commandbars", (from our own GIS.se member Kirk Kuykendall, no less) which looks promising but I can't see how to translate the CreateObject call to .NET, and crucially, it doesn't look like you can actually get an ICommandBar reference from its items.

Any pointers would be greatly appreciated.

Best Answer

I think you are already on the right track. You should use the CommandBars property of IDocument to get a collection of all the tool bars available to a document such as you have seen in the "List ArcMap Visible Toolbars" snippet. But that snippet is coming at things from slightly different angle as it is finding command bars based on items in a category.

I think in your case you should just call CommandBars to get the collection of CommandBars (effectively unfiltered) and then iterate over the collection to get each ICommandBar item. Then you can call the Find method on each ICommandBar to find your ICommandItem instance and then do whatever you need to it.