delphi how to delete a one-time dynamic several components dynamically created (release)?

For example TForm1.Button1Click Procedure (Sender: TObject);
var
I: Integer;
LBL: the TLabel;
the begin
for I: =. 3. 1 to do
the begin
LBL: = TLabel.Create (the Application);
lbl.Parent: = Self;
lbl.Caption : = 'LBL' + the IntToStr (I);
lbl.Top: = 175;
lbl.Height: = 75;
lbl.Width: = 75;
lbl.Left: = I + 10 * lbl.Width;
End;
dynamically generated 3 control, but how in the same event (and then tap the button) to delete all of them again (that is, release it!) it?

--------------------------------------------------------

An array to keep these pointers dynamically generated, in order to release later.

Unit Unit1;

interface

uses
the Windows, the Messages, the SysUtils, Variants, the Classes, Graphics, Controls, Forms,
the Dialogs, StdCtrls;

type
TForm1 = class (a TForm)
Button1: the TButton;
Procedure the Button1Click (Sender: TObject);
Private
{Private Declarations}
IsLableCreated : Boolean;
Labels: Array [0..2] of the TLabel;
public
{public Declarations}
End;

var
the Form1: TForm1;

Implementation

{$ R & lt *}. Dfm

Procedure TForm1.Button1Click (Sender: TObject);
var
the I: Integer;
the begin
IF Not IsLableCreated the then
the begin
for the I: = 0 to 2 do
begin
Labels[I]:=TLabel.Create(Self);
with Labels[I] do
begin
Parent:=self;
Caption := 'Label ' + IntToStr(I);
Top := 175;
Width := 75;
Height :=75;
Left := I*Width +10;
end;
IsLableCreated := True;
end;
end
else
begin
for I := 0 to 2 do
Labels[I].Free;
IsLableCreated := False;
end;
end;

end.

----------------------------------------------------------

Set a global boolean variable  tap change its value
var
bnil: = Boolean to false;
Procedure TForm1.Button1Click (Sender: TObject);
the begin
IF bnil = the then to false
// Build the control
bnil: = true; // change the value
else // i.e. = to true bnil;
// release
end;

Guess you like

Origin www.cnblogs.com/jijm123/p/11009022.html