unit Unit1;interfaceuses
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.
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.
begin
bAddLabel[iLoop].free;
end; SetLength( bAddLabel,0);
Project Project1.exe raised exception class EInvalidPointer with message 'Invalid pointer operation'. Process stopped. Use Step or Run to continue.
你是说释放Form时不要对动态控件做任何事情?这样也不行的,一样的错误
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
ArrLable: Array of TLabel;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
I:Integer;
begin
SetLength(ArrLable,3);
For I:=0 to 2 do
begin
ArrLable[I] := TLabel.Create(self);
ArrLable[I].Caption := 'MyLabel'+IntToStr(i);
ArrLable[I].Parent := Self;
ArrLable[I].Visible := true;
ArrLable[I].Top := 50;
if I=0 then
ArrLable[I].Left := 40
else
ArrLable[I].Left := ArrLable[I-1].Left +100;
end;
update;
end;
上面是我写的测试,不用释放的,你试试
//下标从0开始!