Solved

Delete Component

Posted on 2004-10-20
15
236 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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