Possible Bug in ComboBox

Hi all,

I’ve been working on my plugin today and just got started with ComboBox, when I noticed a peculiar behavior: it seems like the setSelectedItemIndex() method will return an ID that is one greater than the desired ID. Digging through the code, I think I may have found the culprit in ComboBox::getItemForIndex (if I’m right that this is actually a bug and not a silly error on my part):

PopupMenu::Item* ComboBox::getItemForIndex (const int index) const noexcept
{
    int n = 0;

    for (PopupMenu::MenuItemIterator iterator (currentMenu, true); iterator.next();)
    {
        auto& item = iterator.getItem();

        if (item.itemID != 0)
            if (n++ == index)
                return &item;
    }

    return nullptr;
}

Because the ComboBox is one-indexed instead of zero-indexed, but our iterating int n begins at 0, item will have an ID == 1 when n is still zero, by way of the use of the postfix. Thus, when n == index, item will already have been incremented and correspond the next item as opposed to the desired item. A simple fix could possibly be:

if (++n == index)

or indeed even

int n = 1;

However, given that ComboBox seems to enforce sequential menu item IDs (I’m not sure of this but it was seemingly happening as I was setting it up today), it may be more beneficial to use:

if (++n == item.itemID)

I only just started using this class today, so I could just be totally confused about how it’s supposed to function. I’m hesitant to even think this is a bug, because it feels like ComboBox simply wouldn’t work properly if that were the case. In either scenario, some help would be appreciated! For now I’m just manually subtracting 1 from my calls to setSelectedItemIndex(), although I’m concerned that may not work out when ID == 1.

An index is a 0 based value that relates to sequence order. An ID is an arbitrary user defined value, not equal to 0. A menu item at index X, will have an ID stored with it.

Got it! My bad, I was just confused by the terminology. Thank you!