When using the DropDown type ComboBox, if the user enters the value listed in non-ComboBox, the value of ComboBox getCursel () is CB_ERR, the associated getLbText () and getlbtextlen () are not working. When DDX, the ComboBox and CString can be copied. Use it to get or set the Dropdown ComboBox text. // get ComboBox text void GetComboBoxString (HWND hWndCtrl, CString & value) {// just get current edit item text (or drop list static) int nLen = :: (hWndCtrl) GetWindowTextLength; if (nLen> 0) {// get known length :: GetWindowText (hWndCtrl, value.GetBufferSetLength (nLen), nLen 1);} else {// for drop lists GetWindowTextLength does not work - assume // max of 255 characters :: GetWindowText (hWndCtrl, value.GetBuffer ( 255), 255 1);} Value.releaseBuffer ();
// set ComboBox text void SetComboBoxString (HWND hWndCtrl, const CString & value) {// set current selection based on model string OutputDebugString ( "SetComboBoxString:" value); if (:: SendMessage (hWndCtrl, CB_SELECTSTRING, (WPARAM) - 1, (lPAram) (lpctstr) value) == CB_ERR) {/ just set the edit text (will be ignored if DropdownList) AFXSetWindowText (hwndctrl, value);}}