Combo boxes have supported incremental searching for quite some time now


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.

Comments (21)
  1. Dave says:

    You didn't forget, you pulled it:

    blogs.msdn.com/…/4580767.aspx

    [Rats, so I wrote this blog entry twice! -Raymond]
  2. Programmerman says:

    Well, good to know that we'll still have blogs to read through 2020 some time, and that's not including all the new topics that come up in the next 7 years.

  3. Joshua says:

    Interesting. For some reason the .NET combo box, which seems to be a simple wrapper around the Win32 combo box didn't have the behavior, and I went way out of may way to add it.

    As it happened, my behavior was better than this behavior anyway, so I'm not interested in trying to hunt it down.

    [The "things to blog about" pile has over 2000 items in it…] Wow long queue.

    [I open it up once every few years just for show.] Sure helps your popularity. Personally I wouldn't have minded seeing a stack of quick/short answers to a bunch of items in the suggestion box again either (you did that once).

    [That's not the queue. The queue is only 18 months long. The 2000 items is the list of topics I haven't written about yet. And the suggestion box is a suggestion box for future topics, not a Q&A box or reddit/AMA. The topic needs to be worth about 300 to 600 words. -Raymond]
  4. John says:

    [or reddit/AMA]

    Speaking of which I'd be fascinated to see you do one! I'd also be interested to see what your future conference speaking engagements are and make an effort to try and see you in person. I'd love to get my copy of 'The Old New Thing' signed by the one and only Raymond Chen.

  5. xpclient says:

    I always wonder if the "Time zone" combo box could have been better designed for incremental searching.

  6. Chriso says:

    Slove is all you need? :D

  7. Spire says:

    It's too bad that Raymond doesn't "star" comments anymore, because Chriso deserves a star.

    Off-topic: Seconding the request that Raymond do a Reddit AMA.

  8. Rob says:

    "I'd love to get my copy of 'The Old New Thing' signed by the one and only Raymond Chen."

    I'm sure Raymond would also love to meet the guy he owes his royalty check to. :)

  9. John (other) says:

    A Reddit AMA for Raymond Chen would be pointless because, as has been demonstrated on this blog, he would not be willing to answer certain questions.  Can't say I blame him, really.  Nobody wants to get a chair thrown at them.  Perhaps after Mr. Chen retires.

  10. Joshua says:

    "he would not be willing to answer certain questions."

    Backbone is a good thing.

  11. Key to this functionality is the CBS_SORT style.

  12. d.garlans says:

    Reddit has a massive anti-corporate and anti-Microsoft bias. Having a Microsoft employee like Raymond do an AMA would, to use one of Reddit's favorite memes, effectively be saying "You're gonna have a bad time."

  13. TehShrike says:

    Now that you've mentioned the size of your backlog, I'm going to be completely inappropriate and post a question in this comment thread that has bugged me for years:

    After opening up the window menu (Alt+Space, or clicking the icon at top-left) and selecting Move, you can not use the mouse to move the window.  Moving the mouse moves the cursor around, but not the window.  However, if you hit an arrow key, the mouse snaps to the window, and moving the mouse moves the window around.

    Why doesn't the mouse start moving the window around as soon as you go into "move mode"?  Why do you have to hit an arrow key first?

    I've been really curious about the details/history of this behavior for as long as I can remember observing it in Windows, which if memory serves, has been at least a decade and a half.

  14. Joshua says:

    @TehShrike: I'm absolutely sure that the move on the context menu exists for the sole purpose of keyboard accessibility. Evey Raymond wasn't there when the decision was made so long ago but he might be able to confirm.

  15. Neil says:

    It may have been too tiny to see, but I couldn't discern a vertical line.

  16. John says:

    @Joshua:  What do you mean by "Backbone is a good thing"?

  17. ComboGuy says:

    I've tried to set the CBS_SORT style dynamically, according to the need for auto-completion. However, I found out the hard way that setting the CBS_SORT style after the combo box is created doesn't actually tell the control to start doing auto-completion, even after clearing the contents and adding new strings.

    I wish there was a list of what styles are meaningful only at creation time.

  18. @John says:

    It means it's good to be principled even when it's unpopular. Metaphorically, having a spine to stand on your own rather than relying on others to hold you up.

  19. John says:

    @John: I know what the expression means.  What I meant was how it related to my statement?  I would love to ask about internal design decisions, office politics, opinions, etc, but obviously he's not going to answer those types of questions (at least not on a public forum).

  20. Ray says:

    @TehShrike

    Probably because the taskbar defaults to the bottom of the screen. So two scenarios:

    1) You want to move using the mouse – so therefore the default position of a window when initiating a move is hanging off the bottom of the screen. Doesn't sound good.

    1) You want to move using the keyboard – "Aaaargh! Where did my window move to? Oh great it's all the way over there. I'm using my keyboard to move it, why did it snap across 3 screens to where my mouse is?"

  21. ender says:

    @Ray: except that this behaviour hasn't changed at least since Windows 3.1, where there was no multimonitor support.

Comments are closed.

Skip to main content