Link to home
Start Free TrialLog in
Avatar of mi6agent
mi6agent

asked on

Delete Component

I have a panel on which i have placed several components.  What i want to do is go through the panel and (1) Free the component and (2) set it to nil.  At the moment i am using the following code but it does not allow me to set the component to nil:

var
  i:integer;
begin
for i := Panel1.ControlCount - 1 downto 0 do
             With Panel1.Controls[I] do
                   begin
                        Free;
                   end;
end;

The above code works and will delete/free the component but will not set each component to nil

Any ideas?
Avatar of kretzschmar
kretzschmar
Flag of Germany image

use

for i := Panel1.ControlCount - 1 downto 0 do
  FreeAndNil(Panel1.Controls[I]);

meikl ;-)
anyway, this will not nil any reference-variable you may have anywhere
Once you start freeing components this way, the variables that are pointing to these components are still unaware of their state. If you want to free them and assign nil to them, there's only one way:

begin
  Label1.Free;
  Label1 := nil
  Edit1.Free;
  Edit1 := nil;
  ...etc.

Why? Because the object itself doesn't know which variables are referencing it. There could be just one variable or there might even be thousands of variables all pointing to the same control. Only the developer knows which variables are pointing at the objects thus you have no option but to free them one by one.
Avatar of geobul
geobul

Hi,

What actually do you want to set to nil?

var
  i:integer;
begin
for i := Panel1.ControlCount - 1 downto 0 do
              With Panel1.Controls[I] do
                   begin
                        Free;
                   end;
ShowMessage(IntToStr(Panel1.ControlCount)); // will report 0 controls
end;

Regards, Geo
meikl, your code won't work because FreeAndNil expects a variable only. Panel1.Controls[I] is not a variable.

If we are talking about variables referencing these controls then Alex is absolutely correct.
Avatar of mi6agent

ASKER

meikl - sorry, should have mentioned i use Delphi 4 Pro which does not have FreeAndNil

Workshop_Alex / meikl - good point.  Any quick way around this or is it just plain old "set each one to nil one by one"
> What actually do you want to set to nil?

All of the components that were on the panel - ie: free and nil them so if i then use something like If assigned(label1) it will show that label1 is not in a "created" state.
>meikl, your code won't work because FreeAndNil expects a variable only.
missed this geo, thanks ;-)
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks all - some very good points you all have given.  Workshop_Alex - ouch! i see your point on the ugly way to do it.

I have raised the points so i can give you the original 125 points each with a grade A.

Again, thanks to all of you for the input and help.
That's very generous of you. Thanks.
Very generous indeed. :-) Thanks.
shall i say similar?

thanks, glad that we helped you

meikl ;-)