Combo boxes have supported incremental searching for quite some time now

Raymond Chen

Back in August 2007, I promised to post a program the following day but it appears that I never did. Oops. I discovered this as I went through my “things to blog about” pile and figured better late than never. Though five years late is pretty bad.

Here’s a program which fills a combo box with some strings.

#include <windows.h>
#include <windowsx.h>
static LPCTSTR rgpszList[] = {
    TEXT("Austria"),
    TEXT("Belgium"),
    TEXT("Bulgaria"),
    TEXT("Cyprus"),
    TEXT("Czech Republic"),
    TEXT("Denmark"),
    TEXT("Estonia"),
    TEXT("Finland"),
    TEXT("France"),
    TEXT("Germany"),
    TEXT("Greece"),
    TEXT("Hungary"),
    TEXT("Ireland"),
    TEXT("Italy"),
    TEXT("Latvia"),
    TEXT("Lithuania"),
    TEXT("Luxembourg"),
    TEXT("Malta"),
    TEXT("Netherlands"),
    TEXT("Poland"),
    TEXT("Portugal"),
    TEXT("Romania"),
    TEXT("Slovakia"),
    TEXT("Slovenia"),
    TEXT("Spain"),
    TEXT("Sweden"),
    TEXT("United Kingdom"),
};
INT_PTR CALLBACK DlgProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg) {
    case WM_INITDIALOG:
        for (int i = 0; i < ARRAYSIZE(rgpszList); i++) {
            SendDlgItemMessage(hdlg, 100, CB_ADDSTRING, 0,
                               (LPARAM)rgpszList[i]);
        }
        return TRUE;
    case WM_COMMAND:
        if (GET_WM_COMMAND_ID(wParam, lParam) == IDCANCEL) {
            EndDialog(hdlg, 0);
        }
        break;
    }
    return FALSE;
}
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev,
                   LPSTR lpCmdLine, int nShowCmd)
{
    DialogBox(hinst, MAKEINTRESOURCE(1), NULL, DlgProc);
    return 0;
}
// scratch.rc
#include <windows.h>
1 DIALOG 50, 50, 185, 98
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_DLGFRAME | WS_SYSMENU
BEGIN
    COMBOBOX 100,7,40,150,300,CBS_DROPDOWNLIST | CBS_SORT | WS_VSCROLL
END

Run this program and start typing: “S”-“L”-“O”-“V”-“E”… Hey, look, the combo box is performing incremental search and once you hit the “E”, it selected Slovenia, the first item in the list which begins with the letters S-L-O-V-E.

Wait a few seconds, and try it again. This time, type “S”-“P”, and hey look, it selected Spain. You didn’t have to go through all those other “S” countries to get to it.

If you hit F4 to open the combo box and then type “S”-“L”-“O”-“V”-“E”, observe that there is a tiny vertical line that tells you where you are in the incremental search string.

As I noted some time ago, the incremental search resets after four times the double-click time, or two seconds by default.

Note: The “things to blog about” pile has over 2000 items in it, so there really isn’t much need for a Suggestion Box, but I open it up once every few years just for show.

0 comments

Discussion is closed.

Feedback usabilla icon