wxComboBox with AutoComplete get selection returns -1 Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
sw
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sat Mar 16, 2019 8:09 pm

wxComboBox with AutoComplete get selection returns -1

Post by sw »

I have a wxComboxBox created with the wxCB_DROPDOWN style flag. I have enabled auto complete via

Code: Select all

bool wxTextEntry::AutoComplete(const wxArrayString& choices);
and it works as expected.

So when a user types the dropdown appears with suggestions and that is fine. But when selecting a option either by clicking on it or pressing enter on the selection, the resultant call afterwards to GetSelection() returns -1.

I haven't been able to find much information about this apart from a StackOverflow post for wxPython of which the solution did not help me. I feel like I missed something...
Last edited by sw on Thu May 28, 2020 5:49 pm, edited 1 time in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxComboBox with AutoComplete get selection

Post by doublemax »

The list of strings for the autocomplete can (and usually does) contain strings that are not among the list of choices in the control. If you selected one of those, it would be excepted that GetSelection() returns -1. But the same happens if the string happens to be one of the "stock" choices.
Use the source, Luke!
sw
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sat Mar 16, 2019 8:09 pm

Re: wxComboBox with AutoComplete get selection

Post by sw »

The list of string choices are being populated from a database so I'm not seeing any of the "stock" choices in the control. Even in the autocomplete. It's just returning matches based on the wxArrayString choices I passed into the method originally.

If I type into the control and press enter on the entry or use the mouse to select the option, GetSelection() returns -1.

If I press the arrow on the dropdown and navigate the full list using the mouse and select a entry, then GetSelection() returns the correct index of the item in question...

I've done this for a entry using both of these methods. The former returns -1 and the latter the correct index...
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxComboBox with AutoComplete get selection returns -1

Post by doublemax »

Like i said, this is normal. Selecting something from the autocomplete list is the same as typing the same string char-by-char on the keyboard. Even if the content of the textfield happens to match one of the internal choices, this will not be reflected by GetSelection(). Only using the internal choice of the control will set the selection index.
Use the source, Luke!
sw
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sat Mar 16, 2019 8:09 pm

Re: wxComboBox with AutoComplete get selection returns -1

Post by sw »

So is there a way I can get the index of the value selected via the autocomplete?

Out of curiosity, why is this normal behavior?

Just to clarify on the latter case I did there. What I meant is that I was browsing the full list, I meant browsing the full list. Not the autocomplete list, i.e. I didn't start typing and select a match from the autocomplete list. But I understand what you mean.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxComboBox with AutoComplete get selection returns -1

Post by doublemax »

Search the list of choices for the value of the text field.

Code: Select all

int selection = cb->GetStrings().Index( cb->GetValue() )
From the top of my head, untested.
Use the source, Luke!
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxComboBox with AutoComplete get selection returns -1

Post by doublemax »

What I meant is that I was browsing the full list, I meant browsing the full list. Not the autocomplete list, i.e. I didn't start typing and select a match from the autocomplete list.
In that case GetSelection() should work, and it did when i tested it. What platform and wxWidgets version are you using?

Is the autocomplete even relevant in this case? Does it work, when you don't enable autocomplete?
Use the source, Luke!
sw
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sat Mar 16, 2019 8:09 pm

Re: wxComboBox with AutoComplete get selection returns -1

Post by sw »

Indexing the list of strings returns the correct index. Thank you doublemax.

Yes, selecting from the whole dropdown "manually" returns the correct index via GetSelection().

Well, the autocomplete is there for a usability perspective since the wxComboBox contains 100+ entries and typing in to narrow the result helps to find your option in the dropdown.

I was struggling with obtaining the index when selecting a entry via autocomplete.
Post Reply