Reputation: 607
In my application I need show a data in multiple forms that these forms exactly have same VCLs, events and procedures, because the amounts of these forms are depending on to my application, So I can't create all of them in design-time.
Or How can I make a copy of a form multiple times in run-time?
Upvotes: 3
Views: 2675
Reputation: 451
to create undetermined number of forms you can use this code ..
private
MyForm: array of TForm;
procedure TForm1.CreateForms(Sender: TObject);
begin
SetLength(MyForm, Length(MyForm) + 1);
MyForm[Length(MyForm) - 1] := TForm1.Create(Self);
MyForm[Length(MyForm) - 1].Name := 'Form' + IntToStr(Length(MyForm));
MyForm[Length(MyForm) - 1].Caption := 'Form' + IntToStr(Length(MyForm));
MyForm[Length(MyForm) - 1].Show;
end;
Upvotes: 0
Reputation: 47704
Assuming your form is declared as TForm2, you can easily create say 10 instances of it like this:
var
myForms: TArray<TForm2>;
I: Integer;
begin
SetLength(myForms, 10);
for I:=0 to 9 do begin
myForms[I] := TForm2.Create(Application); // Application will free the forms on exit
myForms[I].Show;
end;
end;
If you don't need to access the form instances you can omit the array completely and write:
var
I: Integer;
begin
for I:=0 to 9 do begin
TForm2.Create(Application).Show; // Application will free the forms on exit
end;
end;
Upvotes: 7