Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

free components at runtime 1

Status
Not open for further replies.

DelphiAaron

Programmer
Jul 4, 2002
826
AU
something so simple i cant do.

how do i free all components created at runtime ?

this gives a list index out of bounds error if theres more than 1 control.

var i:integer;
begin
for i:= 0 to ScrollBox1.ControlCount -1 do
ScrollBox1.Controls.free;
end;


Aaron Taylor
John Mutch Electronics
 
typical, you always find the answer 2 seconds after posting the question.

for anyone interested its

for I := ScrollBox1.ComponentCount - 1 downto 0 do
ScrollBox1.Controls.free;

Aaron Taylor
John Mutch Electronics
 
or you can do this (I dislike for-loops :) :

Code:
while ScrollBox1.ComponentCount > 0 do
 FreeAndNil(ScrollBox1.Controls[0]);

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Using FreeAndNil() function assures that memory is actually released by the memory manager...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Whats the problem with for-loops daddy?
Controllable, Optimised, etc..

Steve: Delphi a feersum engin indeed.
 
I don't have anything against for-loop really. but there are cases where they can be avoided. I saves me to declare an index variable in some routines, which is a bit faster (no stack frame).

I work a lot with 0 index based lists and I always use the same method.

consider this code :

Code:
 ...
 for Index:=0 to SomeList.Count-1 do
  begin
   dosomework(Index);
  end;

sometimes I forgot do the -1 part (which caused me alot of headaches in BIG loops, although this is an obvious error)

so I use this standard method:

Code:
 ...
 Index:=SomeList.Count;
 while Count > 0 do
 begin
  Dec(Index);
  dosomework(Index);  
 end;

Writing code this way prevented me from writing incorrect code.

I am not saying I'm against using for-loops , for me, it's more like a habit not using them anymore, I think...

My 2 cents,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
forgot saying one thing in my previous post,

with for loops, you NEED to check the List.Count property, because when Count is 0, you'll get a rangecheck error on the for-loop. this check is automatically done using the while loop...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top