for I:=0 to ControlList.Count-1 do begin TControl(ControlList[I]).Free; end; ControlList.Free;或者: for I:=ControlList.Count-1 downto 0 do begin TControl(ControlList[I]).Free; ControlList.Delete(I); end;
代码如下: for i:= 0 to Panel1.ControlCount - 1 do begin if (Panel1.Controls[i].ClassType = TLabel) then Panel1.Controls[i].Free else Continue; end;
错了,不能这样写,因为随着释放Panel.ControlCount会减少,释放到后来就越界了! for i:= 0 to Panel1.ControlCount - 1 do begin if (Panel1.Controls[i].ClassType = TLabel) then Panel1.Controls[i].Free else Continue; end; 这样试试: count := Panel1.ControlCount; for i:= count - 1 downto 0 do begin if (Panel1.Controls[i].ClassType = TLabel) then Panel1.Controls[i].Free end;
或者这样写也行。 var count:integer; var i:integer; count:=panel1.ControlCount; for i:=0 to count do begin if panel1.controls[i].classtype=TLabel then Panel1.Controls[i].Free count:=panel1.ControlCount; end;
TControl(ControlList[I]).Free;
end;
ControlList.Free;或者:
for I:=ControlList.Count-1 downto 0 do begin
TControl(ControlList[I]).Free;
ControlList.Delete(I);
end;
for i:= 0 to Panel1.ControlCount - 1 do
begin
if (Panel1.Controls[i].ClassType = TLabel) then
Panel1.Controls[i].Free
else
Continue;
end;
begin
if (Panel1.Controls[i].ClassType = TLabel) then
Panel1.Controls[i].Free
else
Continue;
end;
这样试试:
count := Panel1.ControlCount;
for i:= count - 1 downto 0 do
begin
if (Panel1.Controls[i].ClassType = TLabel) then
Panel1.Controls[i].Free
end;
var count:integer;
var i:integer;
count:=panel1.ControlCount;
for i:=0 to count do
begin
if panel1.controls[i].classtype=TLabel then
Panel1.Controls[i].Free
count:=panel1.ControlCount;
end;
同意Voidest(白乌鸦)的写法
一致通过!
你在调试的时候跟踪看一下就知道了!