Back


TListBox

A TListBox is used to show your program users a list from which they can select items, they can add delete individual items etc.

You can use Columns to specify the amount of columns you want in your TListBox.
Setting the TListBox Columns property to zero will cause the TListBox to scroll the list vertically, using a number above zero will cause the TListBox to scroll horizontally.
You use ItemIndex to indicate which item in the list is selected and Items uses a TStringList object to fill it with values.
You can select multiple items in a TListBox with two Boolean properties they are: 
MultiSelect and ExtendedSelect.
If you set ExtendedSelect to True your program users can then hold down Shift or Ctrl and use the mouse to select multiple items in the TListBox.
The MultiSelect property will determine if the list is to support multiple selections.
SelCount is used to return the number of currently selected items in the TListBox.
To arrange the contents of the TListBox alphabetically you use Sorted.

Have a look at the examples below to learn more about what a ListBox can do.

`Clearing the contents of a TListBox:

procedure TForm1.Button1Click(Sender: TObject);
begin
    ListBox1.Clear;
end;

When you have a TListBox and you would like a word in the list to appear visible in the ListBox you can use this code:

procedure TForm1.Button1Click(Sender: TObject);
begin
    with ListBox1 do
      begin
         TopIndex := Items.IndexOf('Three');
      end;
end;

In the ListBox we have a list of words:

One
Two
Three
Four
Five
Six
Seven  and so on...

After clicking on Button1 the ListBox will scroll to the word 'Three'.
The TopIndex property is used to ensure that a certain line is visible in the ListBox.
So the code above will force the ListBox to show the line that has the word 'Three' on it.

To change all the text in a TListBox to capitals (UpperCase) do this:

procedure TForm1.Button2Click(Sender: TObject);
var TheCount : Integer;
begin
    for TheCount := 0 to ListBox1.Items.Count - 1do
      begin
          ListBox1.Items[TheCount] := UpperCase(ListBox1.Items[TheCount]);
      end;
end;

Finding A Word In A TListBox:

Add the words

One
Two
Three
Four
to a TListBox

Add a button to Form1.
Double-click on the button and then add this code:

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
    if ListBox1.Items.IndexOf('Three') > -1 then
     ShowMessage('Found it!')
   else
     ShowMessage('Not Found!');
end;

Run the program and you will find that the code found the word you wanted.

To Find A Word In A TListBox Then Delete It:

Add the words

One
Two
Three
Four
to a TListBox

Add a button to Form1.
Double-click on the button and then add this code:

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
    with ListBox1.Items do
        if ListBox1.Items.IndexOf('Four') > -1 then
        Delete(IndexOf('Four'));
end;

Deleting an Item from a TListBox With A Mouse Click:

procedure TForm1.ListBox1Click(Sender: TObject);
begin
    ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
When you click on an item in the list, it will be deleted.

Adding the contents of Edit1 to a TListBox:

procedure TForm1.Button1Click(Sender: TObject);
begin
    ListBox1.Items.Add(Edit1.Text);
end;

Changing a TListBox columns using a SpinEdit:

procedure TForm1.Button1Click(Sender: TObject);
begin
    ListBox1.Columns := SpinEdit1.Value;
end;
Or you could do this:

Adding Columns to a TListBox:

procedure TForm1.Button1Click(Sender: TObject);
begin
    Listbox1.Columns := 2;
end;
You can also do this by changing the Columns property in the Object Inspector.

Selecting a line in a TListBox:

procedure TForm1.Button1Click(Sender: TObject);
begin
    Listbox1.items.Clear; 
    Listbox1.Items.Add('New Zealand');
    Listbox1.Items.Add('Australia');
    Listbox1.Items.Add('USA');
    Listbox1.Items.Add('England');
    Listbox1.ItemIndex := 2;
end;
First the ListBox is cleared, then the list of words are added, then 'USA' is selected.

Loading and Saving a file with a TListBox:

procedure TForm1.Button1Click(Sender: TObject);
begin
    Listbox1.Items.LoadFromFile('c:\Listitems.txt');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    Listbox1.Items.SaveToFile('c:\savedfile.txt');
end;

Sorting a TListBox:

procedure TForm1.Button1Click(Sender: TObject);
begin
    Listbox1.Sorted := True;
end;
You can also do this by changing the Sorted property in the Object Inspector.

Creating a list of strings in a TListBox.

procedure TForm1.Button5Click(Sender: TObject);
var ListStrings : TStringList;
begin
    ListStrings := TStringList.Create;
    ListStrings.Add('This is');
    ListStrings.Add('my');
    ListStrings.Add('list of strings.');
    ListBox1.Items.AddStrings(ListStrings);
    ListStrings.Free;
end;

Adding Fonts to a TListBox:

procedure TForm1.Button1Click(Sender: TObject);
begin
    ListBox1.Items := Screen.Fonts;
end;

Adding 1 to 100 into a TListBox:

procedure TForm1.Button1Click(Sender: TObject);
var I : Integer;
begin
    for I := 1 to 100 do
      ListBox1.Items.Add(IntToStr(I));
end;

Adding 100 sentences to a TListBox:

procedure TForm1.Button1Click(Sender: TObject);
var I : Integer;
begin
    for I := 1 to 100 do
      ListBox1.Items.Add('Tracy got 100 points.');
end;

Inserting a Yellow Ellipse into a TListBox:

procedure TForm1.Button2Click(Sender: TObject);
begin
    ListBox1.Canvas.Brush.Color := clYellow;
    ListBox1.Canvas.Ellipse(1, 1, 20, 20);
end;
Make sure ListBox1.Style := lbOwnerDrawFixed;


Back