20

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

(Not the integer index)

BIBD
  • 15,107
  • 25
  • 85
  • 137

7 Answers7

33

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
  • 30,088
  • 6
  • 78
  • 89
  • 2
    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 Jan 27 '09 at 18:52
9

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
  • 369
  • 1
  • 3
  • 1
    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 Jan 28 '09 at 19:46
5

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

edit: too slow!

Joe
  • 41,484
  • 20
  • 104
  • 125
1
CComboBox   m_CComboBox;
CString currentValue;


m_CComboBox.GetWindowTextA(currentValue);
cout<<"My current Value is: <<currentValue<<endl;

Have to work with MFC in 2022 .....

Sia
  • 11
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 04 '22 at 23:10
0

CB_GETCURSEL return the integer index CB_GETLBTEXT returns string at CB_GETCURSEL

demonplus
  • 5,613
  • 12
  • 49
  • 68
Michael
  • 106
  • 3
0
char buffer[MAX_COMBO_STRING_LENGTH];

int index= static_cast<int>(SendDlgItemMessage(hDlg, ID_COMBO_BOX, CB_GETCURSEL, 0, 0));
SendDlgItemMessage(hDlg, ID_COMBO_BOX, CB_GETLBTEXT, index, (LPARAM)(LPCTSTR)buffer);

//buffer contains the selected text unless CB_GETCURSEL returned CB_ERR (-1) to the index
theBigCheese88
  • 442
  • 4
  • 16
0

There are several approaches to this.


Map to Variable

When you use a combo you can map it to a variable.

  • If your combo is a drop list it maps to an int.
  • If your combo is a drop down it maps it to a CString.

Direct Acces

As others have said, when your combo is a drop down you can think of it as having two the controls (list and edit). Use GetWindowText To access the edit control text.


Using Macros

There are many helper macros you can use. And there is one for getting the text (ComboBox_GetText macro). Saves you needing to map control to variable.

It really depends on your needs and usage of the combo control.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164