Component Enhancement:  TComboBoxPlus (Rev 2.1) 
By: David Richards <moriarty@foothill.net>
 

< screen shots coming soon >


Dave has done it again -- this time with a TComboBox enhancement.  Have you ever noticed what a hassle it is to implement an OnDrawCell event handler just to change a small attribute such as font color?  This certainly becomes a pain if needed for multiple ComboBoxes.  While cutting and pasting between event handlers works, it is certainly not very maintainable, especially if you need to make a small change everywhere.  Fortunately, TComboBoxPlus eases this hassle by wrappiing the OnDrawCell implementation in a concise OO fashion.  At the heart of Dave's component is the TComboCells class which internally maintains the important attributes of each item (something even an advanced class such as TListItem lacks), including:
 

            __property  TAlignment  Alignment
     __property  TColor      Color
     __property  bool        Enabled
     __property  int         FontSize
     __property  AnsiString  FontName
     __property  TColor      FontColor
     __property  bool        FontBold
     __property  bool        FontItalic
     __property  bool        FontUnderline
     __property  bool        FontStrikeOut
     __property  int         Height
     __property  int         Index
     __property  AnsiString  Text


TComboBoxPlus
Ver. 2.0



Introduction
 

TComboBoxPlus is a TComboBox descendant that allows the user to customize the appearance of the individual items in the drop-down list. Font characteristics, background color and text alignment may be set on an individual item basis. As the box is user-drawn, style selection is limited to OwnerDrawFixed and OwnerDrawVariable. In short, this means that the box is to be used for information display/item selection only - the user cannot enter text into the Edit portion of the control.

All cell contents can be programmed at design time, through the use of a cell editor. The new property 'Cells' has a property editor available through the Object Inspector, and the context pop-up for the component contains a component editor item which accesses the same cell editor. Double clicking on the component in design mode will also bring up the cell editor. The component can be fully loaded and configured at design time. It can even be duplicated via cutting and pasting with the clipboard - all cell contents are copied along with the visual component.

Item characteristics can be changed at run time through the new 'Cells' property. This wraps a TList descendant that maintains pointers to the associated TComboCells objects. Casting is built in, so a typical change of the font color, for instance, of the third item down in the list of a component called 'Box1' would be coded as 'Box1->Cells->Items[2]->Font->Color = clRed'.

I hope to write a short article describing the internal workings of this component, in particular the implementation of the component editor, the property editor, and the use of the streaming mechanism to store the cell data. I'll also include the source for a previous version of the component, identical in capability except without design time programmability. This shows pretty clearly the changes necessary to implement the new features. This article should be appearing at Bytamin-C by mid-November 99.
 



 Download TComboBoxPlus (includes full source code)
TComboBoxPlus ver 2.1 -- (20 KB)