Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Deleting runtime generated buttons from TPanel

Tags:

delphi

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.

like image 501
davykiash Avatar asked Dec 17 '22 13:12

davykiash


1 Answers

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;
like image 97
Mikael Eriksson Avatar answered Feb 03 '23 23:02

Mikael Eriksson