Search code examples
c++arraysconstructorlistboxwxwidgets

How to construct a wxListBox with option of adding entries, without knowing the number in advance?


I have have a wxListBox, which is constructed like this :

wxString choices[8] =  {"0", "1", "2", "3", "4", "5", "6", "7" };

m_listboxSorted = new wxListBox( panelOne, ListBoxOneID,wxPoint(40,250), wxSize(260,125), 8, choices, wxLB_SINGLE|wxLB_HSCROLL, wxDefaultValidator );

This kind of initialization makes room for 8 pre-defined entries in the list. But adding any extra ones ( 9th... ) results in a crash.

How should I correct this code, if I want the list to be initialized with these 8 pre-defined entries, but with possibility of adding 9th, 10th... So as many as the program will require, not predetermined ?

EDIT : Some extra code to hopefully help find the problem

static unsigned int k = 0 ;
m_listboxSorted->SetString(k, "some text");
k++;

This code is part of the handler function which ( on button clicked ) does some processing and also adds an entry to the list box. And apart from the constructor :

wxListBox *m_listboxSorted = new wxListBox;

That is all the code that I see has anything to do with the list box.


Solution

  • You can't use SetString() to add a string to the listbox, it can only be used to modify an existing string (just think about it: what would SetString(10, "whatever") do for an empty listbox, what would be shown in the first 10 items before the string "whatever" in the 11th line?).

    You should use Append() instead. This also will free you from the need to track the index manually (of course, you could always just get it from the listbox itself using its GetCount()).