wxComboBox and sharing a new lesson

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

wxComboBox and sharing a new lesson

Post by softport »

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.
Last edited by softport on Sat Dec 15, 2012 4:49 pm, edited 1 time in total.
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxComboBox and sharing a new lesson

Post by doublemax »

Yes, that's a common mistake. Thanks for posting this.
Use the source, Luke!
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

Re: wxComboBox and sharing a new lesson

Post by softport »

Thanks doublemax,

I've been thinking about a really dumb analogy to working with derived classes...

Say you just built a pond in your back yard, and you would really like to have a duck swimming in it. You go and buy a duck, open the instructions, and all it tells you is how to make it quack. That's nice, but that's not why you got the duck. You keep trying to make it swim, but always end up all wet jumping in the pond to rescue it.

Finally one day you decide to go to the library, and find out all you can about ducks. You discover that ducks have a parent family called Anatidae. And there you find the instructions on how to make it swim! Great! You go home to try it, and sure enough you finally get the duck to swim, wondering why the original instructions didn't mention anything about it. Could there be something else it could do? It's got wings, so it should be able to fly, but there was no mention of this under 'Anatidae'. Now you know the drill, so you go back to the library, and discover that Anatidae have a parent family called birds. Soon you have your duck doing all sorts of tricks, and your neighbors aren't laughing at you anymore.
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW
Forbin
Earned a small fee
Earned a small fee
Posts: 21
Joined: Mon Oct 31, 2016 7:26 pm

Re: wxComboBox and sharing a new lesson

Post by Forbin »

softport wrote:Soon you have your duck doing all sorts of tricks, and your neighbors aren't laughing at you anymore.
I'm late to the game, but swimming, quacking, flying ducks doing tricks for the neighbors is brilliant. It has been a long time since I've read such an amusing object-orientation analogy! :lol:
Post Reply