procedurefreeChildControls(myControl : TControl; freeThisControl: boolean);var
i : integer;
Item : TControl;
beginif Assigned(myControl) thenbeginfor i := (myControl.ControlsCount - 1) downto0dobegin
Item := myControl.controls[i];
if assigned(item) then
freeChildControls(item, childShouldBeRemoved(item));
end;
if freeThisControl then
FreeAndNil(myControl);
end;
end;
functionchildShouldBeRemoved(child: TControl): boolean;
begin//consider whatever conditions you need//in my test I just checked for the child's name to be layout1 or label1
Result := ...;
end;