views:

865

answers:

4

How can I get the value (string) of the current selection in a combobox?

(Not the integer index)

+5  A: 

There might be a better way (my MFC is a bit rusty), but it seems like you should be able to call CComboBox::GetLBText(), passing it the current selection using CComboBox::GetCurSel().

Andy
Hmm, that works. Thanks
You're probably saying "Hmm" because you were trying to see the new value in a CBN_SELCHANGE handler. That's triggered /before/ the change is committed, but GetLBText does already have access at this point..
Aidan Ryan
A: 

Use GetLBText, passing in the index and a CString object.

edit: too slow!

Joe
A: 

CB_GETCURSEL return the integer index CB_GETLBTEXT returns string at CB_GETCURSEL

--

Michael

Michael
A: 

A plain old GetWindowText works, too.

Edit: As ajryan points out, GetWindowText actually doesn't work in a CBN_SELCHANGE handler, because the new selection has taken effect but the window text hasn't been updated with the text of the new selection when the WM_COMMAND is sent.

Joel
Not in ON_CBN_SELCHANGE
Aidan Ryan
Hmmm. Looks like you're right. I guess I've never tried that before. A little surprising, because there's usually a "changing" notification for before changes are committed and a "changed" notification for afterward, but that doesn't seem to be the case for combo boxes.
Joel