
ou create an "owner-draw" ListBox or ComboBox when you want to bypass the control's automatic item display in order to do something special, such as display an image for each item or display a list in which the items aren't all the same size. The .NET framework makes it simple to create these custom item lists. In this article, you'll see how to populate list and combobox controls with items you draw yourself.
The only thing you need to do to create an owner-drawn list or combo box is to set the
DrawMode property to either
OwnerDrawFixed, or
OwnerDrawVariable. The
DrawMode property has three possible settings:
Normal (the default), in which the system handles displaying the items automatically;
OwnerDrawFixed, which you should use when you want to draw the items yourself, but they're all the same height and width; and
OwnerDrawVariable, which you use to draw items that vary in height or width. When you select the
OwnerDrawFixed setting, you must implement a
DrawItem method, which the ListBox calls whenever it needs to draw an item. When you select the
OwnerDrawVariable setting, you must implement both the DrawItem and a MeasureItem method. The MeasureItem method lets you set the size of the item to be drawn. When you select the
Normal setting, the system does not fire either the MeasureItem or the DrawItem methods.
There are a couple of restrictions. You can't create variable-height items for multicolumn ListBoxes, and CheckedListBoxes don't support either of the owner-drawn DrawMode settings.