Home > Back-end >  How to add multiple-column Items to TListBox?
How to add multiple-column Items to TListBox?

Time:07-24

In a 32-bit VCL Application in Windows 10 in Delphi 11.1 Alexandria, I am trying to add multiple-column Items to TListBox. The CHM Libraries Reference for VCL in the Vcl.StdCtrls.TCustomListBox.Items topic has the following tip:
enter image description here

So I created the following VCL Application test project:

DPR:

program TListBoxMultiColumn;

uses
  Vcl.Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

PAS:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.Add('First Column'^I'Second Column');
  ListBox1.Items.Add('1'^I'2');
  ListBox1.Items.Add('4'^I'5');
end;

end.

DFM:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'TListBox MultiColumn Test'
  ClientHeight = 191
  ClientWidth = 368
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  Position = poScreenCenter
  PixelsPerInch = 120
  TextHeight = 20
  object ListBox1: TListBox
    Left = 0
    Top = 0
    Width = 241
    Height = 191
    Margins.Left = 4
    Margins.Top = 4
    Margins.Right = 4
    Margins.Bottom = 4
    Align = alLeft
    Columns = 2
    ItemHeight = 20
    TabOrder = 0
    ExplicitHeight = 413
  end
  object Button1: TButton
    Left = 260
    Top = 20
    Width = 94
    Height = 31
    Margins.Left = 4
    Margins.Top = 4
    Margins.Right = 4
    Margins.Bottom = 4
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end

However, the result is not what is promised in the documentation:

enter image description here

So how can I add multiple-column Items to a TListBox?

CodePudding user response:

You must set the TabWidth property to a suitable, large enough value:

ListBox1.TabWidth := 100;
ListBox1.Items.Add('First Column'^I'Second Column');
ListBox1.Items.Add('1'^I'2');
ListBox1.Items.Add('4'^I'5');

Bonus information: You may wonder why ^I is used here. Well, since I is the 9th letter in the English alphabet, ^I is equal to #9, that is, the tabulator character.

I would write this

ListBox1.TabWidth := 100;
ListBox1.Items.Add('First Column'#9'Second Column');
ListBox1.Items.Add('1'#9'2');
ListBox1.Items.Add('4'#9'5');

Actually, the current version of the documentation states

Tip: If you have a list box with tab stops enabled (TabStop property) and you want to add data to specific columns, you can set the TabWidth property to obtain a list box in which individual lines can be displayed in columns, as long as they use tabs in their text, as shown in the snippet below (notice #9 is the tab character).

This is a better description, since it mentions the TabWidth property, uses #9 instead of ^I, and doesn't misuse the word "parameter". However, its reference to TabStop is utterly nonsense. The TabStop property is about the form's tab order.

  • Related