Solved

Delete Component

Posted on 2004-10-20
15
234 Views
Last Modified: 2010-04-05
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?
0
Comment
Question by:mi6agent
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12357519
use

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

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12357529
anyway, this will not nil any reference-variable you may have anywhere
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12357544
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.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12357559
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
0
 
LVL 17

Expert Comment

by:geobul
ID: 12357612
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.
0
 

Author Comment

by:mi6agent
ID: 12357635
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"
0
 

Author Comment

by:mi6agent
ID: 12357791
> 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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 12357960
>meikl, your code won't work because FreeAndNil expects a variable only.
missed this geo, thanks ;-)
0
 
LVL 17

Accepted Solution

by:
geobul earned 125 total points
ID: 12357974
I see. Try this (Edit1 is on Panel1):

procedure DeleteComponent(AControl: TControl);
var c: TComponent;
begin
  if Assigned(AControl) then begin
    AControl.Parent := nil;
    c := AControl.Owner;
    if Assigned(c) then c.RemoveComponent(AControl);
    AControl.Free;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  i:integer;
begin
  for i := Panel1.ControlCount - 1 downto 0 do
    with Panel1.Controls[I] do
      DeleteComponent(TControl(Owner.FindComponent(Panel1.Controls[I].Name)));

  // for testing only
  if Assigned(Edit1) then ShowMessage('Assigned')
  else ShowMessage('nil');
end;

Regards, Geo
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 125 total points
ID: 12358022
>If assigned(label1) it will show that label1 is not in a "created" state

well, if you do not want the advice from alex,
then you could instead of assigned following

if assigned(findcomponent('label1')) then
  //label1 already exist
else
  //label1 must be created

usual findcomponent searches for the componentname,
so if you create a new one, you should name it as well
(if you drop compnents at designtime the name of the component
and the name of the reference-variable are the same)

this is just a workaround

meikl ;-)
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 125 total points
ID: 12358164
Well, if you want to set all variables to nil then there is one way, but it's a bit rediculous to do. What you should do in such a case is create an array of pointers to objects. For every variable you want to set to nil, add a pointer to this array pointing to the address of this variable. Fill this array during runtime on your FormCreate method or perhaps even earlier. Then, whenever you want to free and set all those variables to nil, just walk through this list. :-)

Example:

type
  PObject = ^TObject;
  TVarList: array of PObject;
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
  private
    VarList: TVarList;
    procedure Clear;
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetLength(VarList, 2); // 2 variables...
  VarList[0]^ := @Label1;
  VarList[1]^ := @Edit1;
end;

procedure TForm1.Clear;
var
  I: Integer;
begin
  for I := Low(VarList) to High(VarList) do begin
    VarList[I]^.Free;
    VarList[I]^ := nil;
  end;
end;

Not tested, though so it's probably flawed. But it's just to show what kind of weird code you would have to start to write compared to:

procedure TForm1.Clear;
begin
  FreeAndNil(Label1);
  NreeAndNil(Edit1);
end;

Now, it is up to you. Personally I would hate to have to keep track of addresses of variables so I can access those variables while it is perfectly clear to me where those variables are. My example is just plain ugly. So ugly that I won't insult my compiler by testing it. :-P
0
 

Author Comment

by:mi6agent
ID: 12359239
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.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12359694
That's very generous of you. Thanks.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12359869
Very generous indeed. :-) Thanks.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12360195
shall i say similar?

thanks, glad that we helped you

meikl ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now