Access Addin Combobox from Button

4276
6
05-20-2011 01:20 PM
TereseRowekamp
New Contributor III
I have an addin that contains a combobox, a button and a tool. I want the button to be able to update the combobox but cannot figure out how to properly get a reference to the combobox. Can someone give me a VB example?

Thanks.
0 Kudos
6 Replies
KenBuja
MVP Esteemed Contributor
You can reference another add in component with the following code

Dim pUID As New ESRI.ArcGIS.esriSystem.UID
Dim pCommandItem As ESRI.ArcGIS.Framework.ICommandItem

pUID.Value = My.ThisAddIn.IDs.DrawTool 'DrawTool is the name of my addin tool...yours will be different
pCommandItem = m_application.Document.CommandBars.Find(pUID, False, False)
0 Kudos
TereseRowekamp
New Contributor III
Thanks for the response, Ken.

I've added the code below to my button OnClick to find the ComboBox:

        Dim pUID As New ESRI.ArcGIS.esriSystem.UID
        Dim pCommandItem As ESRI.ArcGIS.Framework.ICommandItem

        pUID.Value = My.ThisAddIn.IDs.LayerCombobox
        pCommandItem = My.ArcMap.Application.Document.CommandBars.Find(pUID, False, False)


But now what? The command item I'm looking for is a combobox. I want the button to update the contents of the combobox.

I can't access the combobox directly from the button to clear its items and re-add them, and the only method on ICommandItem that seems to be appropriate is Execute, but what gets executed for a Addin Combobox?
0 Kudos
JeffreyHamblin
New Contributor III
Taking a guess here after looking at the API docs:

ICommandItem has a Command member, which is an ICommand.

Then there is IComboBox.

And finally IComboBoxHook which has methods to work with the list.

Here is some info:
Creating combo boxes


Hope that helps.

-Jeff
0 Kudos
TereseRowekamp
New Contributor III
I figured it out. Here's what I did:

In my combobox code, I have a sub named UpdateLayerList which adds layers in the data frame that contain a required field to the combobox.

In my button code, I added the following to OnClick

        Dim cbx = ESRI.ArcGIS.Desktop.AddIns.AddIn.FromID(Of clsLayerCombobox) My.ThisAddIn.IDs.clsLayerCombobox)

        cbx.UpdateLayerList()


I had originally been close to the solution using the following
        Dim cbx As ESRI.ArcGIS.Desktop.AddIns.ComboBox = ESRI.ArcGIS.Desktop.AddIns.AddIn.FromID(Of clsLayerCombobox)(My.ThisAddIn.IDs.clsLayerCombobox)

which did not cause any compile errors, but at run-time it would cause an exit from the code without any error messages. It was when I couldn't get that to work that I posted my question and then tried the approach using ICommandItem.

When that didn't work either, I searched some more and found sample code which I used and it works great. The difference is here
Dim cbx =
instead of
Dim cbx As ESRI.ArcGIS.Desktop.AddIns.ComboBox =

I may not understand exactly what is being returned here, but the code works, my layer list can get updated from my button correctly, and I'm happy. (In case you're wondering, there had been a problem in the past trying to update the layer list by listening to an update event for changes to the map, which is why we went with a button that the user clicks to update the list).

I also used similar code to get to the text in another combobox (which I'm using as a text box on my toolbar) so it's been a good afternoon.

Thanks for your responses.
0 Kudos
TereseRowekamp
New Contributor III
One line gets wrapped and a ( was dropped, so I want to be clear. The lines I added were:

Dim cbx = ESRI.ArcGIS.Desktop.AddIns.AddIn.FromID(Of clsLayerCombobox) (My.ThisAddIn.IDs.clsLayerCombobox)

cbx.UpdateLayerList()
0 Kudos
davidrenz
New Contributor III
very nice, guys! thanks for posting this!

here is how i did it in C#

        protected override void OnClick()
        {


            CTargetLayer oCombo = (CTargetLayer)ESRI.ArcGIS.Desktop.AddIns.AddIn.FromID<CTargetLayer>(ThisAddIn.IDs.CTargetLayer);
            oCombo.refreshLayers();

            System.Windows.Forms.MessageBox.Show("Done");
        }
0 Kudos