4
votes

I have several TPanels that are populated with buttons at runtime. However the code below that i use to free my buttons from their parent panels sometimes generates access violation errors.

procedure TfrmTakeOrder.FreeItemButtons(buttons : array of TButton);
var
  cnt,i : integer;
begin

  for i := 0 to gridLayoutItems.ControlCount - 1 do
    begin
      buttons[i].Free;
      buttons[i] := nil;
    end;

end;

Is there a better way to do this?Please keep in mind that other Panels have buttons too and I would like to have a "localised" freeing of the buttons that wount intefer with other panels.

8

8 Answers

7
votes

It looks to me like you are trying to remove all buttons from a TPanel and that panel only contains buttons.

Try this:

while gridLayoutItems.ControlCount > 0 do
  gridLayoutItems.Controls[0].Free;
3
votes

If:

  • that panel has only buttons, and
  • your array has all the buttons on several panels.

Then use:

var
  I: Integer;
  Button: TControl;
begin
  for I := GridLayoutItems.ControlCount - 1 downto 0 do
  begin
    Button := GridLayoutItems.Controls[I];
    Button.Free;
    { Find the index of Button in the Buttons array };
    { Erase that item from the array };
  end;
end;

But in this scenario, it is much more handy to have a TList for Buttons instead of an array, becasue then the code becomes:

var
  I: Integer;
  Button: TControl;
begin
  for I := GridLayoutItems.ControlCount - 1 downto 0 do
  begin
    Button := GridLayoutItems.Controls[I];
    Button.Free;
    Buttons.Remove(Button);
  end;
end;
1
votes

Maybe it would be better to use Length(buttons) - 1 instead of gridLayoutItems.ControlCount - 1 which could be different.

1
votes

You have to correct the bounds of your for-loop like the other answers state. One more thing:

How do you create the buttons? If you create the buttons with an owner, i.e.

buttons [i] := TButton.Create (Panel);

then you must not free them manually. The owner takes care of that. In this case just use

buttons [i] := TButton.Create (nil);
0
votes

Yes. Use for i := Low(buttons) to high(Buttons) do

0
votes

In more recent versions of Delphi (like XE5), Free does not exist any more. I solved the problem using Destroy instead of Free.

0
votes

If you want to free only buttons from parent panel:

var i : integer
begin
i := 0;
while Panel1.ControlCount > i do
  if Panel1.Controls[i] is TButton then
     Panel1.Controls[i].Free
  else inc(i);
end;
0
votes

you can use this code

for i := 0 to ComponentCount-1 do
 begin
  if ( Components[ i ] is TPanel ) then
  (Components[ i ] as TPanel ).free;
 end;