Get all items from a ListBox

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
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Get all items from a ListBox

Post by Wanderer82 »

Hi

I have used GetSelections to get the indexes of all the selected items in a ListBox. But now I need to get the indexes of all items, selected or unselected. I didn't find a function for that or maybe I just missed it.

Thanks in advance
Thomas

EDIT:

I just use GetString(int) now. But I have a problem: I try to fill a wxArrayString in a loop but always get an error:

Code: Select all

 	wxArrayString Printers;
        int items_count = ListBox2->GetCount();
        int i = 0;
        
        do
        {
           Printers = ListBox2->GetString(i); 
        }
        while(i < items_count);
 

Error:

no match for 'operator=' (operand types are 'wxArrayString' and 'wxString')|
candidate is:
wxArrayString& wxArrayString::operator=(const wxArrayString&)|
no known conversion for argument 1 from 'wxString' to 'const wxArrayString&'|
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Get all items from a ListBox

Post by doublemax »

You can't assign a wxString to a wxArrayString, they're different things. You want to add the wxString to the array.

Code: Select all

Printers.Add( ListBox2->GetString(i) );
The do-while loop is no good here, as it will execute the body once even if there is no item in the list (and you're not incrementing i). Use a good-ole for-loop.

But then again, you don't even need the loop:

Code: Select all

printers = ListBox2->GetStrings()
This method is hidden in one of the base classes of wxListBox:
https://docs.wxwidgets.org/trunk/classw ... 60d307d7c0
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Get all items from a ListBox

Post by Wanderer82 »

Oh, it's so easy. I even did use the "Add" version but didn't get it to work as I thought that the item's index wasn't just the simple number from 0 to xy.

For the incremetion of i... that was just missing because I reproduced the code for the forum (as I had already found another solution with an ordinary wxString array). So I just forget while being a bit in a hurry ;-)

But the do-while-loop thing... I don't even know why I thought I had to use a do-while loop. I'm glad you made that clear so I could change my code in another part as well [-o<
Post Reply