Get text from CComboBox
Solution 1
You need to call GetWindowText
before the dialog is destroyed. A good place to do that would be in the DoDataExchange
member function of the dialog.
Edit: You can also get an error if the CComboBox object hasn't been attached to the actual window. This also typically occurs in DoDataExchange
.
Solution 2
You can get the combo box text inline with the following code:
CString cStr;
CWnd* Pfield = GetDlgItem(MY_COMBOBOX_ID);
Pfield->GetWindowText(cStr);
JHowzer
Updated on June 08, 2022Comments
-
JHowzer almost 2 years
I have a CDialog (
myDialogBox
) that has aCComboBox
member (myComboBox
). My goal is to get the user-typed text (preferably as aCString
) from the edit portion of a dropdown styleCComboBox
. But, I cannot seem to get it to work.I've tried using
myComboBox.GetWindowText(myString)
. But whenever I run it in debug mode, my code breaks atASSERT(::IsWindow(m_hWnd))
within theCWnd::GetWindowText()
function.I've also tried
myDialogBox.GetDlgItemText(MY_COMBOBOX_ID, myString)
, which breaks down in a similar way.Additionally, I've tried:
COMBOBOXINFO info; memset(&info,0,sizeof(info)); myComboBox.GetComboBoxInfo(&info); LPTSTR str = new TCHAR[::GetWindowTextLength(info.hwndItem)]; ::GetWindowText(info.hwndItem,str,::GetWindowTextLength(info.hwndItem));
But, it doesn't seem to be populating my
info
variable correctly.Could someone point me in the right direction, please? What am I doing wrong? Any suggestions?
EDIT: Just in case it might help understand my ultimate goal, I am trying to have a combo-box where that can help a user pic and choose from a listing of strings. Nonetheless, if he/she does not want any on the dropdown-list, he can come up with his own string. I'd like a way to receive his user-typed string.
-
JHowzer over 11 yearsWhat's odd is that it's happening even when I haven't destroyed the dialog box. To check, I made a function that occurs
OnEditChangeMyComboBox
, which only does the following:UpdatesData(TRUE)
, then it tries to get the text. It doesn't destroy the dialog or anything. -
JHowzer over 11 yearsAs far as
DoDataExchange
, I haveCDialog::DoDataExchange(pDX); DDX_Control(pDX, MY_COMBOBOX_ID, myComboBox);
. Is that sufficient to attach it to the dialog? -
Mark Ransom over 11 years@JHowzer, yes that should be sufficient. However the error you're getting is that there's no window attached to the object. I'm out of ideas on how that could occur.
-
acraig5075 over 11 yearsMake sure the ID property for the control in the Dialog Editor is still in fact
MY_COMBOBOX_ID
and that it hasn't accidently been changed. If the ID is now different, then the solution will still build but the combobox won't have a valid window handle. -
JHowzer over 11 yearsI realized I was missing a CString variable tied to the combo-box. This also required adding
DDX_CBString(pDX, MY_COMBO_ID, myComboString);
toDoDataExchange
. After that, IUpdateData(TRUE);
when selecting or editchange events occur. Thank you both for your help. -
Mark Ransom over 11 years@JHowzer it's good that you got it to work. I wouldn't have expected your solution to be any more successful than the others based on the error you were getting.