Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 2 of 2 total
Thread Freeing controls via Controls[] array throws error
Thu, May 24 2012 12:22 AMPermanent Link

Rick

Tim

I have a panel (Panel1) on the main form along with two buttons. The first
button dynamically adds child panels to Panel1. The second button removes
(frees) the child panels from Panel1. The OnClick definitions are as
follows:

procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
for i:=1 to 10 do
with TPanel.Create(Panel1) do
 begin
  Width:=100;
  Height:=100;
  Top:=Round(Random()*Panel1.ClientHeight);
  Left:=Round(Random()*Panel1.ClientWidth);
  ShowShadow:=True;
 end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
while Panel1.ControlCount<>0 do
TPanel(Panel1.Controls[0]).Free;
end;

Button1 works correctly. Clicking Button2 however throws the following
errors:

IE8: Cannot assign to a function result Line: 12873
Firefox: Invalid assignment left-hand side Line: 12873

If the OnClick for Button2 is changed to the following then the child panel
removal works correctly.

procedure TForm1.Button2Click(Sender: TObject);
var
p: TPanel;
begin
while Panel1.ControlCount<>0 do
begin
 p:=TPanel(Panel1.Controls[0]);
 p.Free;
end;
end;

Should it be possible to free an object directly from the Controls[] array?

--
Rick

Fri, May 25 2012 4:15 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Rick,

<< Should it be possible to free an object directly from the Controls[]
array? >>

No, I'll make sure that this is fixed.

Tim Young
Elevate Software
www.elevatesoft.com
Image