简体   繁体   中英

Get selected item in dropdown Win32

How do I get the selected value of the current dropdown every time there are changes in the dropdown list?

case WM_COMMAND:

        break;

A combo box notifies its parent about changes in selection by sending a CBN_SELCHANGE message. You can get the currently selected item index by sending a CB_GETCURSEL message. The item text is available by sending a CB_GETLBTEXT message:

case WM_COMMAND:
    switch(HIWORD(wParam))
    {
        case CBN_SELCHANGE:
        {
            HWND const control{ (HWND)lParam };
            int const index{ ::SendMessage(control, CB_GETCURSEL, nullptr, nullptr) };
            if (index == CB_ERR)
            {
                // Handle error
                return 0;
            }
            // [optional] Retrieve text of selected item
            int const len{ (int)::SendMessage(control, CB_GETLBTEXTLEN, (WPARAM)index, nullptr) };
            if (len == CB_ERR)
            {
                // Handle error
                return 0;
            }
            std::vector<wchar_t> buffer(len + 1);
            ::SendMessageW(control, CB_GETLBTEXT, (WPARAM)index, (LPARAM)buffer.data());

            return 0;
        }
        case default:
            break;
    }
    break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM