Mega Code Archive

 
Categories / Delphi / Examples
 

How to display columns in a simple listbox

The TabWidth property of the TListbox component sets the number of dialog base units, usually pixels, for each tab character. Set it, to say, a half of the ListBox' width to display two columns. When adding strings to the ListBox, use the tab character (^I) at the desired position: ListBox1.Items.Add('Column1'^I'Column2'); The imperfection of such approach is that the width of column is not set automatically depending on the width of the string displayed, which 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;