Columns in a ListBox
I often met in books and other sources an example of creating a component that allows display multicolumn strings in ListBox. But if you look at source of TListBox component, you will see that it already has property TabWidth, derived from TCustomListBox, that allows to use columns. I didn't find it in Delphi 2 online help, though it presents in Delphi 2 too. TabWidth sets the number of dialog base units, usually pixels, for each tab character. Set it, to say, a half of ListBox' width to display two columns. When adding strings to ListBox, use tab character (^I) in wanted position:
ListBox1.Items.Add('Column1'^I'Column2');
The imperfection of such approach is that the width of column does not set automatically depending on width of string displayed, what is simple to improve. Let's look at TextWidth method of TCanvas class. It returns a width in pixels of a string passed as a parameter. Then we can write:
with ListBox do begin W := Canvas.TextWidth(Str); if W > TabWidth then TabWidth := W; end;