Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Delete Component

Posted on 2004-10-20
15
237 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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