pallagommosa
pallagommosa

Reputation: 558

How to get the currently selected text in a CComboBoxEx?

My first approach to the problem was to call the GetWindowsText method on the CComboBoxEx control, but I found that there is no associated text. After analyzing the control with Spy++ and reading some documentation on CComboBoxEx, I realised that these type of controls are only the parent of a classic ComboBox:

Spy's screenshot

I tried using the GetLBText() method on the child ComboBox, passing GetCurSel() as an argument, but I only get some wrong text (the correct text should be "English"):

Wrong text

Am I missing something? Thanks in advance!

Upvotes: 2

Views: 704

Answers (2)

Andrew Truckle
Andrew Truckle

Reputation: 19157

What you want to do is map the control to a int variable using Class Wizard:

Class Wizard

Now it is easy to access the selected text at any time. You need to use the GetItem function. For example (code not tested):

COMBOBOXEXITEM cmbItem;
CString strText;

cmbItem.mask = CBEIF_TEXT;
cmbItem.iItem = m_cbItemIndex;
cmbItem.pszText = strText.GetBuffer(_MAX_PATH);
m_cbMyCombo.GetItem(&cmbItem);
strText.ReleaseBuffer();

In short, you need to use the COMBOBOXEXITEM and initialise it with the right flags to state what information you want to get from the extended combo. That, and the item index. Job done!


I realise that you have your own inherited class, but the mechanics are the same. You don't use GetLBText. You use the structure with the index and GetItem to get the selected text.

Upvotes: 2

pallagommosa
pallagommosa

Reputation: 558

In the end I managed to retrieve the correct name; as you can see in the image below, the ComboBox is only a child of a CombBoxEx32:

enter image description here

I retrieved the pointer to the parent ComboBoxEx32 from the child ComboBox, and searched for the text this way:

CString szText;
CComboBoxEx cbParentCombo ;
cbParentCombo.Attach( GetParent()->GetSafeHwnd()) ;
cbParentCombo.GetLBText( GetCurSel(), szText) ;
cbParentCombo.Detach() ;

My mistake was that I was calling GetLBText() directly from the child ComboBox, instead of the parent CComboBoxEx; because of that, all I was getting was some random gibberish. GetLBText() was indeed the correct solution.

Upvotes: 1

Related Questions