unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
private
FStringList: TStringList;
function GenerateRandomItem: TShape;
function GenerateText(aShape: TShape): string;
procedure ShowInformation;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
const
ClickCount: Integer = 0;
var
Something: TShape;
S: string;
i: Integer;
begin
// === First click
// Create some items and store the information to FStringList
if ClickCount = 0 then
begin
for i := 0 to 15 do
begin
Something := GenerateRandomItem;
S := GenerateText(Something);
Something.Hint := '#' + i.ToString + ' - ' + S;
FStringList.Add('Item #' + i.ToString + ' is a ' + LowerCase(S) + '.');
FStringList.Objects[i] := Something;
end;
ShowInformation;
Button1.Caption := 'Click me AGAIN !';
Inc(ClickCount);
Exit;
end;
// === Second click
// Delete all the items in the FStringList
if ClickCount = 1 then
begin
repeat
Something := (FStringList.Objects[0] as TShape);
FreeAndNil(Something);
FStringList.Delete(0);
until FStringList.Count <= 0;
ShowInformation;
Button1.Enabled := False;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
FStringList.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FStringList := TStringList.Create;
end;
function TForm1.GenerateRandomItem: TShape;
var
NewShape: TShape;
begin
NewShape := TShape.Create(Self);
with NewShape do
begin
Parent := Self;
Width := 50;
Height := 50;
Left := Random(Self.Width - 50);
Top := Random(Self.Height - 90);
ShowHint := True;
case Random(3) of
0: Shape := stRectangle;
1: Shape := stCircle;
2: Shape := stTriangle;
end;
case Random(3) of
0: Brush.Color := clYellow;
1: Brush.Color := clGreen;
2: Brush.Color := clRed;
end;
end;
Result := NewShape;
end;
function TForm1.GenerateText(aShape: TShape): string;
var
S: string;
begin
S := '';
case aShape.Brush.Color of
clYellow: S := 'Yellow';
clGreen: S := 'Green';
clRed: S := 'Red';
end;
case aShape.Shape of
stRectangle: S := S + ' Retangle';
stCircle: S := S + ' Circle';
stTriangle: S := S + ' Triangle';
end;
Result := S;
end;
procedure TForm1.ShowInformation;
var
S: string;
i: integer;
begin
S := '';
for i := 0 to FStringList.Count-1 do
S := S + FStringList[i] + LineEnding;
Label1.Caption := S;
Label1.Visible := True;
Height := Label1.Height + 250;
end;
end.