Adding both text and an ID value to a VB6 combobox

2019-01-28 04:37发布

问题:

I am currently trying to add to my VB6 combobox using the AddItem method. This works, however, I want to display text in the drop down but I need to pass the ID of that text.

Is there a way to accomplish this by using the AddItem method?

回答1:

It can't be done in the AddItem method but it's fairly easy to do it immediately after, using the NewIndex property, as long as the ID is a numeric value:

With Combo1
    For i = 16 To 34
        .AddItem "Item " & i
        .ItemData(.NewIndex) = i
    Next
End With


回答2:

As the ID was not numeric I didn't use the solution above.

I had to create a type that had a "desc" and an "cod" and then create an array of that type.

I then used the ListIndex of the drop down (populated by the array) to get the element value which contained the id.

Private Type T_arrType
    cod As String
    dsc As String
End Type


dim x as integer
x = cbo.listIndex
msgbox(strArr(x).cod)
msgbox(strArr(x).dsc)


标签: vb6