Delete TEdit created at run time

0

In the click of the button I create some Edits ... So far everything works normally, but when I try to delete the edits created by clicking another button, not all are deleted.

Ex. I created 10 edits fields, when I click to delete all at once, only 5 are deleted. Here are the codes (inclusion and exclusion respectively):

for x := 0 to (NumReg -1) do
begin
  ArrayEdit[x] := TEdit.Create(Self);
  ArrayEdit[x].Parent := Self;
  ArrayEdit[x].Name := 'edtPreco'+ IntToStr(x+1);
  ArrayEdit[x].Left := 265;
  ArrayEdit[x].Top := 300 + x * 25;
end;

var Component: TComponent; 
begin 
  for Component in Self do 
    if Component is TEdit then
      TEdit(Component).Free;
end;
    
asked by anonymous 08.08.2016 / 23:11

2 answers

2
procedure TForm2.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := ComponentCount - 1 downto 0 do
  begin
    If (Components[i] is TEdit) then
      TEdit(Components[i]).Destroy;
  end;
end;
    
08.08.2016 / 23:43
0

Try this:

while (Form1.ComponentCount > 1) do 
begin
  if (Form1.Components[0] is TEdit) then
    TEdit(Form1.Components[0]).Free;
end;
    
08.08.2016 / 23:25