Combobox.additem create 2 new items

Combobox.additem create 2 new items

TONELLAL
Collaborator Collaborator
529 Views
7 Replies
Message 1 of 8

Combobox.additem create 2 new items

TONELLAL
Collaborator
Collaborator

Hello,

Everything is in the title...

I've created a combobox on a ribbon. This combobox contains 3 values, for example A, B, C.

I need to add a new value : D, to this combobox.
I use combobox.additem ("D")

The result is : "A, B, C, D, D" instead of "A, B, C, D"

The value combobox.listcount is 3 before the Additem, and is 4 after the Additem, but in the list 2 lines are added...

If I run "combobox.removeitem(combobox.listcount)", to remove the last line, the 2 last lines are removed, and listcount is only decreased by 1

0 Likes
530 Views
7 Replies
Replies (7)
Message 2 of 8

jjstr8
Collaborator
Collaborator

I can't reproduce your issue. Can you post your code? This is what I'm using to test. I run AddPanelToToolsTab once, then AddToComboBox.

Sub AddPanelToToolsTab()
    Dim partRibbon As Ribbon
    Set partRibbon = ThisApplication.UserInterfaceManager.ribbons("Part")
    Dim toolsTab As RibbonTab
    Set toolsTab = partRibbon.RibbonTabs.Item("id_TabTools")
    Dim newPanel As RibbonPanel
    Set newPanel = toolsTab.RibbonPanels.Add("Test", "ToolsTabTestPanel", "SampleClientId", "id_PanelP_ToolsTest")
    Dim cmbDef As ComboBoxDefinition
    Set cmbDef = ThisApplication.CommandManager.ControlDefinitions.AddComboBoxDefinition("TestCombo", "cmbTest1", kQueryOnlyCmdType, 100, "testcode", "Test combobox")
    cmbDef.AddItem ("A")
    cmbDef.AddItem ("B")
    cmbDef.AddItem ("C")
    Dim cmbCmdCtrl As CommandControl
    Set cmbCmdCtrl = newPanel.CommandControls.AddComboBox(cmbDef)

End Sub

Sub AddToComboBox()
    Dim partRibbon As Ribbon
    Set partRibbon = ThisApplication.UserInterfaceManager.ribbons("Part")
    Dim toolsTab As RibbonTab
    Set toolsTab = partRibbon.RibbonTabs.Item("id_TabTools")
    Dim newPanel As RibbonPanel
    Set newPanel = toolsTab.RibbonPanels("ToolsTabTestPanel")
    Dim cmbDef As ComboBoxDefinition
    Set cmbDef = ThisApplication.CommandManager.ControlDefinitions("cmbTest1")
    cmbDef.AddItem ("D")
End Sub

 

0 Likes
Message 3 of 8

TONELLAL
Collaborator
Collaborator

I tested your code, it works...

I simplified the sub AddTo Combobox to keep only :

    Dim cmbDef As ComboBoxDefinition
    Set cmbDef = ThisApplication.CommandManager.ControlDefinitions("cmbTest1")
    cmbDef.AddItem ("D")

 

In my code, the combobox already exist on a tab, so I just modified it using the command AddItem().

The full code of modifying is : 

Sub AddToComboBox2()
    Dim cmbDef As ComboBoxDefinition
    Set cmbDef = ThisApplication.CommandManager.ControlDefinitions("Styles_de_nomenclature")
    cmbDef.AddItem ("A")
End Sub

The original list :

TONELLAL_0-1742813401853.png

 

I run AddToCombox2() :

TONELLAL_1-1742813500820.png

I run again AddToCombobox2, replacing A by B :

TONELLAL_2-1742813569627.png

 

0 Likes
Message 4 of 8

jjstr8
Collaborator
Collaborator

Yes, most of my AddToComboBox wasn't needed. It was mostly copy/paste from the other routine. I can't see what could cause this. I would set a breakpoint at the beginning of your code and single-step through to see if AddItem is being called twice for some reason. The way VBA integrates in Inventor, you can still interact with Inventor as you step through your code.

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor

Is your code for adding an item to the ComboBox being called to run by an 'event handler'?  If so, then you may need to check the 'BeforeOrAfter' variable's value, which usually represents the 'EventTimingEnum' aspect of that Event, so that you are only reacting to that event during either its 'kBefore' or 'kAfter' timing stage, instead of during both stages.  Just a thought that came to mind.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 8

TONELLAL
Collaborator
Collaborator

@jjstr8 :

I already debugged step by step. The 2 lines are created by the command combobox.additem, and the macro is ran only once.

If I create the same macro using combobox.RemoveItem, then debug step by step, same thing : the 2 lines are removed by the single RemoveItem.

 

@WCrihfield :

On a combobox, there are only the events OnSelect or OnHelp. So, no EventTimingEnum

 

I found a workaround : delete the combobox from the panel, then create and insert another one, but it is not a good solution 😞

0 Likes
Message 7 of 8

jjstr8
Collaborator
Collaborator

What version of Inventor are you on? I tested it on 2024. How is the AddToComboBox routine called? Is there more of your code that you can post?

0 Likes
Message 8 of 8

TONELLAL
Collaborator
Collaborator

I'll try to post the relevant code tomorrow. Globally, the combobox definition (so the original list) is defined in class.initialize, then the AddToComboBox is called by the OnSelect event, depending on the selected value. It seems the OnSelect event is recursively ran when it call AddToCombobox, but when I debug step by step I don't see several calls.

0 Likes