Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   How to create Eg. Ten instaces of the same form? (https://www.delphipraxis.net/73267-how-create-eg-ten-instaces-same-form.html)

pastra 14. Jul 2006 17:38


How to create Eg. Ten instaces of the same form?
 
Hi I would like to know how to create 10 forms, all created from "Form1"

The forms need to be active at the same time so uniqe names are required..


How to acomplice that?


Thanks..!

mkinzler 14. Jul 2006 17:42

Re: How to create Eg. Ten instaces of the same form?
 
Delphi-Quellcode:
for i := 1 to 10 do
  with Tform1.Create( self) do
  begin
    Name := 'form1_'+IntToStr( i);
    ...
  end;
Or even better store the Instances into an array.

Dax 14. Jul 2006 17:45

Re: How to create Eg. Ten instaces of the same form?
 
Well, first: do not use the global var Form1 in the Form's Unit anymore. Rather use self when you are in the same Unit. If you want to use these Forms from other Forms or Units, you must create a new global var of type array[1..10] of TForm1 to hold the ten Forms for access from outside themselves.

After that, you need an activation-procedure to create an show all the Forms, maybe like this:
Delphi-Quellcode:
procedure ShowAllForms;
var
  i: Integer;
begin
  for i := 1 to 10 do
  begin
    Forms[i] := TForm1.Create(Application); // Forms is your array
    Forms[i].Show;
  end;
end;
When done, you can access the Forms through their index :)

pastra 14. Jul 2006 18:15

Re: How to create Eg. Ten instaces of the same form?
 
Zitat:

Zitat von Dax
Well, first: do not use the global var Form1 in the Form's Unit anymore. Rather use self when you are in the same Unit. If you want to use these Forms from other Forms or Units, you must create a new global var of type array[1..10] of TForm1 to hold the ten Forms for access from outside themselves.

After that, you need an activation-procedure to create an show all the Forms, maybe like this:
Delphi-Quellcode:
procedure ShowAllForms;
var
  i: Integer;
begin
  for i := 1 to 10 do
  begin
    Forms[i] := TForm1.Create(Application); // Forms is your array
    Forms[i].Show;
  end;
end;
When done, you can access the Forms through their index :)


Okay this code look interresting... When trying to compile in (D6) i get: '.' expected but [ found at: Forms[

What am I dooing wrong? Do I need to define some unit in the USES clase?


Creers!

mkinzler 14. Jul 2006 18:17

Re: How to create Eg. Ten instaces of the same form?
 
Have you declared the global Array?

pastra 14. Jul 2006 18:23

Re: How to create Eg. Ten instaces of the same form?
 
Zitat:

Zitat von mkinzler
Have you declared the global Array?

Sorry for NOT reading therrorly enough what you worte..

Got it working nicely by:

Delphi-Quellcode:
var
  Formz: array[1..10] of TForm1;
(Had to use Z insted og s but that is just a minor detail..


Thank's to all for the fast reply's..


Regards Pastra

Peinhard 15. Jul 2006 09:55

Re: How to create Eg. Ten instaces of the same form?
 
A TList or TObjectList is also an appropriate place to store the form instances. And how about a little component that manages most of the tasks involved with that - eg showing forms with certain contents or bring them to front, destroy forms when the calling form ist destroyed or dynamically build a 'Windows' menu on that form etc.


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:32 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz