Frame Replication [Error: Component already exists]

1

I have a form in delphi and wanted to click a button a pre-created frame was added dynamically several times. I'm trying through this code:

procedure TMain.Button1Click(Sender: TObject);
var i:integer; frame:TFrame;
begin

  for i:=0 to 5 do
    begin
      frame := Tframe.create(self);
      with frame do
        begin
        parent:= self;
        left:= 376 + (i*220);
        top:= 136 + (i*332);
        end;
    end;

end;

When running, I'm notified of the following error:

  

"A component named Frame already exists"

I have already looked at several sites and I do not find the solution to this problem. I found an almost identical question on this link but no solution.

    
asked by anonymous 20.12.2016 / 19:23

1 answer

3

A simple solution is to put a name for each frame you create. It looks like this:

procedure TMain.Button1Click(Sender: TObject);
var 
    i:integer; 
    frame:TFrame;
begin    
    for i := 0 to 5 do
    begin
        frame := Tframe.create(self);
        frame.name := 'MeuFrame' + IntToStr(i);
        with frame do
        begin
            parent:= self;
            left:= 376 + (i*220);
            top:= 136 + (i*332);
        end;
    end;    
end;
    
21.12.2016 / 13:16