Frame Replication [Error: Component already exists]

Asked

Viewed 418 times

1

I have a form in Delphi and I wanted to have a pre-created frame added dynamically several times by clicking a button. 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 executing I am notified of the following error:

"A Component named Frame already exists"

I’ve searched a lot of sites, and I can’t find a solution to that problem. I found an almost identical question in this link but unsolved.

  • Your code works perfectly here. There’s something else in your code ?

1 answer

3


A simple solution is to name each frame created. 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;
  • 1

    +1 it would be enough for the guy to have read the error return message: ALREADY EXISTS.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.