1

There is combo box with two items and button on the main window. Combobox:

HWND hCombo;
hCombo = CreateWindow(L"COMBOBOX", L"combobox",
       WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST,
       10, 55, 232, 500, hWnd, 0, hInstance, 0);

const wchar_t *langEnglish = L"English";
const wchar_t *langRussian = L"Russian";
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)langEnglish);
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)langRussian);
SendMessage(hCombo, CB_SETCURSEL, 0, 0);

I am trying to get selected item text in the WndProc by clicking on the button:

case WM_COMMAND:                                            
{
    switch(LOWORD(wParam))
    {  
        case IDC_BUTTON_OK:
            wchar_t buf[10];
            hCombo = GetDlgItem(hWnd, IDC_COMBO);
            GetDlgItemText(hCombo, IDC_COMBO, (LPWSTR)buf, 10);
            MessageBox(hWnd, (LPCWSTR)buf, NULL, MB_OK);
            break;
    }

} break;

I am using breakpoint in the MSVS2010 to see buf variable. It contains chinese symbols!!! Message box shows empty message (With the title "Error"). I want to see english text. What is wrong?

This code

nIndex = SendMessage(hCombo, CB_GETCURSEL, 0, 0);
SendMessage(hCombo, CB_GETLBTEXT, nIndex, (LPARAM)buf);

fills buf with the same chinese symbols

SOLUTION:
hCombo = CreateWindow(L"COMBOBOX", L"combobox", WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST, 10, 55, 232, 500, hWnd, (HMENU)IDC_COMBO, hInstance, 0);

1 Answer 1

1

In order to get currently selected item from CBS_DROPDOWNLIST styled combo box you need CB_GETCURSEL to get selection index and then CB_GETLBTEXT to get the string.

3
  • This code: nIndex = SendMessage(hCombo, CB_GETCURSEL, 0, 0); SendMessage(hCombo, CB_GETLBTEXT, nIndex, (LPARAM)buf); fills the buf with the same chinese symbols
    – NieAR
    Commented Apr 30, 2012 at 7:53
  • The characters you refer to as Chinese are in fact an uninitialized buffer. The first character is perhaps zero and this makes the returned value just an empty string. This in turn has its own reason - wrong handle etc. Commented Apr 30, 2012 at 7:56
  • Well then - you have empty string returned. Why? Wrong handle or no selection (items were removed by the time of the call) etc. Commented Apr 30, 2012 at 8:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.