TListPack (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example requires two edit box controls and a button on the form. The code creates a list object and adds some strings to it. The second string in the list is a nil string. The code counts the number of strings in the list and displays the number in the Edit1 control. The code then packs the list, removing the nil string, and counts the strings in the list again. The second count displays in the Edit2 control.

Code

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  MyList := TList.Create;              { Create a list. }
  MyList.Add(PChar('A string')); { Add a string. }
  MyList.Add(PChar('')); { Add an empty string. }
  MyList.Add(PChar('A third string')); { Add a string. }
  MyList.Add(nil);              { Add nil. }
  MyList.Add(PChar('A fifth string')); { Add a string. }
  MyList.Add(PChar('')); { Add another empty string. }
  MyList.Add(PChar('A seventh string')); { Add a string. }
  Edit1.Text := IntToStr(MyList.Count); { Put count into Edit1. }
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyList.Pack;                         { Pack the list.}
  Edit2.Text := IntToStr(MyList.Count); { Put count into Edit2. }
  Repaint;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  MyList.Free;                         { Free memory for the list. }
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  DisplayTList(MyList);
end;

Uses