March 10th, 2006

Why does the size of a combo box include the size of the drop-down?

Many people are surprised to discover that when you create a combo box (either in code via CreateWindow or indirectly via a dialog box template), the size you specify describes the size of the combo box including the drop-down list box, even though the drop-down list box is not visible on the screen. For example, if you say that you want the combo box to be 200 pixels tall, it will be visible on the screen as a 20-pixel-tall (say) edit control, and when the user drops the list box by clicking on the drop-down arrow, the list box will be 180 pixels tall. This has the unfortunate consequence that if you fail to take this into account and specify in your dialog box template that you wanted a 20-pixel-tall combo box, you will end up with a combo box whose drop-down listbox is zero pixels tall! That’s not a very useful combo box. Those who have switched to version 6 of the common controls library may have noticed that the version 6 combo box detects this common mistake and “auto-repairs” it: It recognizes that the height passed by the creator of the combo box is absurdly small and automatically enlarges it to something more reasonable. This was done at the request of the user interface designers who were fed up with struggling with program after program that set their combo box heights too small and ended up showing uselessly-small combo box drop-down list boxes. Imagine, for example, a “Choose your state” combo box where the drop-down shows only two states at a time! (The change was not made to pre-version 6 combo boxes for compatibility reasons.) But I still haven’t answered the question, “Why does the size of a combo box include the size of the drop-down?” The reason is that the original combo box did not have a drop-down. Originally, a combo box was just an edit control and a list box glued together. (You can still see this “old-timey-style combo box” in Notepad’s Font dialog.) You can think of the original combo box as a modern combo box where the drop-down was pinned open. Under this original design, it was reasonable for the size of the combo box to include both the edit control and the list box, since that’s how much space it took up. When the “drop-down” style of combo box was invented, the designers wanted to make the transition from “old-timey combo box” to “slick new drop-down combo box” as easy as possible, so the sizing behavior was retained so that code and dialog boxes wouldn’t have to change to take advantage of the new drop-down style combo box aside from changing to the CBS_DROPDOWN or CBS_DROPDOWNLIST style in the parameters to CreateWindow or in the dialog template.

And that’s why the size of a combo box includes the size of the drop-down. It’s a chain of backwards compatibility going all the way back to the old-timey days before combo boxes learned how to drop down.

Topics
History

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.