Combo box - behaviour when emptying

Hi Jules,

If a ComboBox is emptied programatically (happens to be essential for my current apps!), it appears that the ComboBox continues to show the text of the last item before the final item was removed [or something like that].

Hoping you can fix this soon! :slight_smile:

All the best,

Pete

Ok, will take a look…

Hmm - if you take a look at ComboBox::clear(), it does clear the box if it’s read-only. It won’t clear it if it’s editable in case the user is in the middle of typing something in there. Is yours editable, or is something else not working?

I’ll double check…!

Right… here we go. :slight_smile:

void ComboBox::clear()
{
 ...
    if (! label->isEditable())
        setSelectedItemIndex (-1);
}

So we enter here with index == -1 …

void ComboBox::setSelectedItemIndex (const int index,
                                     const bool dontSendChangeMessage)
{
    if (getSelectedItemIndex() != index)
   ...
}

We enter here with currentIndex == 0 (the last selected combo box item index in my test):

int ComboBox::getSelectedItemIndex() const throw()
{
    return (currentIndex >= 0 && getText() == getItemText (currentIndex))
                ? currentIndex
                : -1;
}

So we enter here…

const String ComboBox::getItemText (const int index) const throw()
{
    ItemInfo* const item = getItemForIndex (index);

    if (item != 0)
        return item->name;

    return String::empty;
}

Which returns String::empty …

And in turn enter here…

const String ComboBox::getText() const throw()
{
    return label->getText();
}

Which enters this…

const String Label::getText (const bool returnActiveEditorContents) const throw()
{
    return (returnActiveEditorContents && isBeingEdited())
                ? editor->getText()
                : text;
}

Which returns text… which is the last value set from the combo i.e. “Fred”

Because this is different to the empty string returned from String ComboBox::getText() , this ends us returning -1 from here:

int ComboBox::getSelectedItemIndex()
...

Which means that this code check fails (because index == 0, does not equal the -1 returned from getSelectedItemIndex…).

void ComboBox::setSelectedItemIndex (const int index,
                                     const bool dontSendChangeMessage)
{
    if (getSelectedItemIndex() != index)
  ...

… and therefore the label text is not reset within that method.

Hoping that makes sense?

Pete

Ok, gotcha. Will sort that out. Ta!

:slight_smile: