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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to free dynamically created components at runtime? 4

Status
Not open for further replies.

muntz70

Programmer
Dec 5, 2003
25
0
0
US
I'm actually a little embarrased I have to ask this question. Seems simple enought to accomplish, but I seem to be having a brain fart. I've even searched tek-tips and can't find a solution...

I dynamically create 10 labels on a panel at runtime. At some point I need to remove (free) those labels from that panel at runtime also.

So, I was trying to loop though the components on the on the panel and free the component if it is a TLabel. No go.

Below is my code:

try
for x := 0 to (panel1.ComponentCount -1) do begin
if (panel.Components[x] is TLabel) then
TLabel(panel1.components[x]).free;
end;
finally
panel1.Repaint;
end;


I'm sure there is a better way to accomplish this simple task, especially since the above doesn't work anyway.

Thanks..
 
I got it...

I changed my looping structure... instead of:

for x := 0 to (panel1.componentcount -1) do begin

I used:

for x := (panel1.ComponentCount -1) downto 0 do begin

Works great!

 
Yeah, for some reason the loop has to go downwards for this sort of job, god knows why and it's damn annoying the first time you try it.
 
Also, I ended up using ControlCount. ComponentCount did nothing for me (I'm using Delphi 4)

Kemp.. I hate it when I can figure out "how" but don't understand "why". I'll have to test this in Delphi 7 too. I'm curious if it was just a Delphi 4 bug.

 
It happened to me in Delphi 3 and since then I've always gone downwards, so I couldn't tell you off-hand if they fixed it since then.
 
I use this in Delphi 6 with no problems:

Code:
procedure TfrmMain.ClearMainGrids(AForm : TForm);
var
i, j, k : integer;
begin
  With AForm do
  begin
    For i := 0 to ComponentCount - 1 do
    begin
      If Components[i] is TStringGrid Then
        with Components[i] as TStringGrid do
        begin
          for j := 0 to RowCount do
            for k := 0 to ColCount do
              Cells[k , j] := '';
        end;
    end;
  end;
end;

Leslie

In times of universal deceit, telling the truth will be a revolutionary act. - George Orwell
 
Good to know Leslie! Thanks for the input. As soon as I have time I plan on testing my original code in Delphi 7. I have a feeling it will work in version 7.

 
The reason you have to go backwards in the loop is because it moves the component indexs down to 'fill in' for the one you just deleted. This makes the next index incorrect. By going backwards, no moving needs to occur so the indexed components you are looping remains correct throughout the entire loop.
 
ToTheMetal : A very good point and one that I really should have seen before myself
 
ToTheMetal!!!!! You win the prize. I had not thought of that (obviously). Now it all makes sense!

Thanks... and a star to you sir!
 
There is a good? discription of the difference between components and controls in the Delphi help under 'objects components and controls' (D3)
As your panels are visual objects they are likly to be controls decending from components. But you should be a be to cast them as components as in lespaul's example.

The useful function FindComponent has no FindControl equivalent. You just have to cast correctly.




Steve

Life is like a Grapefruit, sort of orangey-yellow and dimpled on the outside, wet and squidgy in the middle, it's got pips inside too. Oh and some people have half a one for breakfast. Ford Prefect.

Want to do more with TGML Download Star
 
That's more good information Steve. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top