Smart List Box with Formatting and In-Line Buttons

The Smart List Box lets you define multiple vertical columns and add font controls to a standard list box facility.

As shown in the screen captures, you can specify the forecolor and backcolor for each column, as well as the font characteristics. You also can specify font characteristics for each cell; that is, the rows do not all have to look the same. You can specify headers and footers, as well; they need not have the same text. Vertical and horizontal scroll bars are automatically generated when necessary. The font codes used are compatible with HTML font codes.

The standard properties and methods still work. So, you can AddItem and RemoveItem, and use ListIndex and ListCount as you would with a normal VB List Box. When you use AddItem, you separate the data for each column with a TAB character [CHR$(9)].

You can specify an HTML font code (colour, bold, italics, and so forth), if you wish, for each piece of column data.

You can have one or more buttons in each cell; when the button is pressed, a ButtonClick event occurs in the calling program. You can specify the alignment of buttons independently of the alignment of the text in the cell. The SmartListBox maintains two ImageLists internally, containing the pictures to be displayed normally. When the button is pressed, a button may be partially displayed and still function as a button. Hidden buttons (due to the column width being such that the button is left or right of the displayed area) are disabled. Pictures are placed into the SmartListBox with the AddImage method, and then referenced with a code similar to an HTML code; you can optionally add an alignment code after the picture reference to force the picture to align at the bottom, centre, or top of the cell in which it is displayed. The picture reference may be a number or a name.

Row and column separator colours can be specified, as well as the thickness of the lines. Unlike the normal VB ListBox, you can change the Style property from Normal to CheckBox (or vice versa) at run time. You also can change the height of any or all lines.

You can set the AllowDuplicates property that will or will not cause an error if a line with a duplicate key (data in the sort column) is entered.

But wait, there’s more!

There is a SortDone event that is fired when sorting is complete. This lets you re-paint line background colours, for example, in the case where you have alternating backgrounds and the sequence is messed up by a column sort.

This program uses my automated debugging technique (basically a replacement for Debug.Print). For details, see my Testing Concepts Web site, and click on Zero Defect Programming. By setting the global variable WantDebug to TRUE, the program will tell you what it is doing, creating a trace log in the C:\ root directory.

Several test harnesses are included in the source code file. Each test harness exercises features of the SmartListBox and lets you play with it to understand how the functions work.

Check box style can be turned on or off at run-time:

An error optionally can be raised if duplicate lines are entered:

Macros can be used in the footers to show count, average, minimum, maximum, and total.

Several test harnesses are included, ranging from simple to complex. The complex one lets you experiment with most properties, and play with using buttons; each button may have an alignment code that may be different from the text alignment.

More by Author

Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Must Read