如此创建的动态控件数组,释放时怎么会出错?如果只创建一个动态控件,这样释放没问题
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
bAddLabel:array of TLabel;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
iLoop :integer;
s :string[11];
begin
SetLength(bAddLabel,3);
for iLoop :=1 to 3 do
begin
bAddLabel[iLoop] := TLabel.Create(self);
Str(iLoop,s);
bAddLabel[iLoop].Parent := Form1;
bAddLabel[iLoop].Height := 60;
bAddLabel[iLoop].Width := 50;
bAddLabel[iLoop].Color := clBlue;
bAddLabel[iLoop].Caption := 'Dynamic create control' + ' ' + s + '个';
bAddLabel[iLoop].Top := Form1.Top + 40;
bAddLabel[iLoop].Left := form1.Left + 30 + (iLoop-1)*bAddLabel[iLoop].Width;
bAddLabel[iLoop].Show;
end
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
iLoop :integer;
begin
for iLoop :=1 to 3 do
begin
if bAddLabel[iLoop] <> nil then
bAddLabel[iLoop].free;
end;
bAddLabel := nil;
end;
end.