wxComboBox and sharing a new lesson
Posted: Sat Dec 15, 2012 1:18 am
Hi, just wanted to post this to give something back, at least to relative newcomers like myself.
Today I needed a drop down list, and wxComboBox looked like it would do. Unfortunately when looking up the help file, or with the code completion, nothing I tried would make the control act like I needed: populate it from empty, one item at a time, from inside a while loop.
In the routine that creates the controls, just above where the combo box is created, I found this line:
wxArrayString arrayStringFor_WxComboBox1;
Maybe I can manipulate the array directly... Nope, just got into more trouble. Here's the lesson I learned (in a post): if you are working with a derived class, look at what features are available for the parent. In this case, one of wxComboBox's parents is wxControlWithItems. Look in the help section for it, and there are all the features you couldn't find for wxComboBox.
wxString str;
For just outside your while loop:
WxComboBox1->Clear();
For adding entries from inside your loop:
WxComboBox1->Append(str);
Once outside your loop, the combo items will be out of sight below the first blank line. To fix this use:
WxComboBox1->SetSelection(0);
In my case I needed to convert some printf statements to strings for the combo box entries:
printf(" Product: %ls\n", cur_dev->product_string);
I ended up with this (more or less):
str.Printf(wxT(" Product: %ls"), cur_dev->product_string);
WxComboBox1->Append(str);
The wxT() around the text in quotes may not be necessary, not sure of that.
Hope this saves a new wxWidgets user some time.
Today I needed a drop down list, and wxComboBox looked like it would do. Unfortunately when looking up the help file, or with the code completion, nothing I tried would make the control act like I needed: populate it from empty, one item at a time, from inside a while loop.
In the routine that creates the controls, just above where the combo box is created, I found this line:
wxArrayString arrayStringFor_WxComboBox1;
Maybe I can manipulate the array directly... Nope, just got into more trouble. Here's the lesson I learned (in a post): if you are working with a derived class, look at what features are available for the parent. In this case, one of wxComboBox's parents is wxControlWithItems. Look in the help section for it, and there are all the features you couldn't find for wxComboBox.
wxString str;
For just outside your while loop:
WxComboBox1->Clear();
For adding entries from inside your loop:
WxComboBox1->Append(str);
Once outside your loop, the combo items will be out of sight below the first blank line. To fix this use:
WxComboBox1->SetSelection(0);
In my case I needed to convert some printf statements to strings for the combo box entries:
printf(" Product: %ls\n", cur_dev->product_string);
I ended up with this (more or less):
str.Printf(wxT(" Product: %ls"), cur_dev->product_string);
WxComboBox1->Append(str);
The wxT() around the text in quotes may not be necessary, not sure of that.
Hope this saves a new wxWidgets user some time.