The following simplification of your setup:

  for i:=1 to 1000
  do begin
    btn:=TButton.Create(nil);
    btn.parent:=panel1;
  end;

// followed by

  i:=panel1.controlcount-1;
  while i >= 0
  do begin
    if( panel1.controls[i] is TButton )
    then panel1.controls[i].free;
    i:=i-1;
  end;

does not appear to have the problem you describe.

Is it possible that freeing your buttons implicitly
removes some other controls from the panel behind your
back?

If so ...  try removing them this way instead:

    i:=0;
    while i < panel1.controlcount
    do begin
      if( panel1.controls[i] is TButton )
      then with panel1.controls[i]
      do begin
        parent:=nil;
        free;
        i:=0;
      end
      else i:=i+1;
    end;


this is less efficient than the other way - but i think
also less vulnerable to surprises from side-effects
of the free.

i'll be interested to hear what the problem turns out
to be.

-ns



----- Original Message -----
From: "Willie" <[EMAIL PROTECTED]>
To: "Multiple recipients of list delphi" <[EMAIL PROTECTED]>
Sent: Thursday, August 09, 2001 7:35 PM
Subject: [DUG]: Control.Free


> I'm banging my head against something hard (very hard) here.
>
> Can anyone see what is wrong with the following code....
>
> First I populate a panel with a number of 1st Class Shape buttons as
per....
>
>   CurrentButton := tfcShapebtn.Create(Nil);
>   with dm.cdsButtons  do
>   begin
>     currentbutton.OnClick := Button1Click;
>     currentbutton.Parent := MainPanel; //
>     currentbutton.Tag := FieldByname('QSButtonID').AsInteger;
>     currentbutton.Name := 'fcShapebtn' +
> IntToStr(FieldByname('QSButtonID').AsInteger);
> etc etc
>
> later on I want to free them all (to populate the panel with another
> different set) .....
>
>   i := MainPanel.ControlCount - 1;
>   while i >= 0 do
>   begin
>     if (MainPanel.Controls[i] is TfcShapebtn) or
>       (MainPanel.Controls[i] is TfcLabel) then
> MainPanel.Controls[i].Free;
>     i := i - 1;
>   end;
>
> this AVs on the free statement for the last ShapeButton control instance
> (which is the first one created)
>
> Thanks
>
> --------------------------------------------------------------------------
-
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to