Using a List Box

A ListBox has a huge number of methods and properties. In our straightforward Lazarus demonstration we show you how to add an item to the list and to remove an item from it. If you add several items you may want to save the new version of the list, so we use the convenient SaveToFile and LoadFromFile methods. We added to the form a ListBox, Edit and four buttons (from the Standard tab). In the Object Inspector we set the Sorted property of the ListBox to true and clicked on the ellipsis button for the Items property to obtain the Strings Editor Dialog. We typed in the items and sorted them. The ItemIndex property equals the zero-based number of the selected item in the list and equals -1 if no item is selected.

Output from program ListBoxDemo

Output from program ListBoxDemo

Our code of the files needed for program ListBoxDemo follows. You can copy the code if necessary, but a you should be able to adapt it for your own use by inspecting the code. (Delphi 7 has a useful DeleteSelected method for a ListBox).

uListBoxDemo.pas

unit uListBoxDemo;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type
  TfrmRoutines = class(TForm)
    btnAdd: TButton;
    btnDelete: TButton;
    btnSave: TButton;
    btnLoad: TButton;
    edtNew: TEdit;
    lboRoutines: TListBox;
    procedure btnAddClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnLoadClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  end; 

var
  frmRoutines: TfrmRoutines;

implementation

{$R *.lfm}

const
  ROUTINES = 'routines.txt';

procedure TfrmRoutines.btnDeleteClick(Sender: TObject);
var
  Deleted : string;
begin
  with lboroutines do
    begin
      if ItemIndex = -1 then
        showMessage('You must select a routine first.')
      else
        begin
          Deleted := Items.Strings[ItemIndex];
          Items.Delete(ItemIndex);
          showMessage(Deleted + ' deleted');
        end;
    end;
end;

procedure TfrmRoutines.btnLoadClick(Sender: TObject);
begin
  if fileExists(ROUTINES) then
    lboRoutines.Items.LoadFromFile(ROUTINES)
  else
    showMessage('Save the file first.');
end;

procedure TfrmRoutines.btnSaveClick(Sender: TObject);
begin
  lboRoutines.Items.SaveToFile(ROUTINES);
  showMessage('Saved to file');
end;

procedure TfrmRoutines.btnAddClick(Sender: TObject);
var
  i : integer;
  Found : Boolean;
  Addition : string;
begin
  Found := false;
  Addition := edtNew.Text;
  i := 0;
  while i < lboRoutines.Items.Count do
    begin
      if UpperCase(Addition) = Uppercase(lboRoutines.Items[i]) then
        Found := True;
      inc(i);
    end;
  if Found = False then
    begin
      lboRoutines.Items.Add(Addition);
      edtNew.Text := '';
      showMessage(Addition + ' added');
    end
  else
    showMessage(Addition + ' is already present.');
end;

end.
      

uListBoxDemo.lfm

object frmRoutines: TfrmRoutines
  Left = 553
  Height = 201
  Top = 189
  Width = 299
  Caption = 'Routines'
  ClientHeight = 201
  ClientWidth = 299
  LCLVersion = '0.9.30'
  object lboRoutines: TListBox
    Left = 0
    Height = 182
    Top = 8
    Width = 120
    Items.Strings = (
      'AssignFile'
      'CloseFile'
      'DateToStr'
      'Halt'
      'IntToStr'
      'LeftStr'
      'Now'
      'Pos'
      'Read'
      'Readln'
      'Reset'
      'Rewrite'
      'RightStr'
      'Sleep'
      'StrToInt'
      'Trim'
      'Val'
      'Write'
      'Writeln'
    )
    ItemHeight = 20
    Sorted = True
    TabOrder = 0
  end
  object edtNew: TEdit
    Left = 128
    Height = 28
    Top = 8
    Width = 84
    TabOrder = 1
  end
  object btnAdd: TButton
    Left = 128
    Height = 25
    Top = 48
    Width = 88
    Caption = 'Add Routine'
    OnClick = btnAddClick
    TabOrder = 2
  end
  object btnDelete: TButton
    Left = 128
    Height = 25
    Top = 88
    Width = 163
    Caption = 'Delete selected routine'
    OnClick = btnDeleteClick
    TabOrder = 3
  end
  object btnSave: TButton
    Left = 128
    Height = 25
    Top = 128
    Width = 75
    Caption = 'Save'
    OnClick = btnSaveClick
    TabOrder = 4
  end
  object btnLoad: TButton
    Left = 128
    Height = 25
    Top = 160
    Width = 75
    Caption = 'Load'
    OnClick = btnLoadClick
    TabOrder = 5
  end
end 
      

ListBoxDemo.lpr

program ListBoxDemo;
uses
  Interfaces, Forms, uListboxDemo;
begin
  Application.Initialize;
  Application.CreateForm(TfrmRoutines, frmRoutines);
  Application.Run;
end.
Programming - a skill for life!

Using widgets (such as list boxes, combo boxes, string grids, DBgrids, charts and maps) and drawing on the canvas