Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How-To ... TForm.Create with a variable?? 1

Status
Not open for further replies.

TimSNL

Programmer
Sep 11, 2001
119
0
0
AU
Hi All.

I have a question about creating an new instance of a form.

First ... background information.
Somewhere in a unit I have defined a from called
Code:
TFrmData
.
I want to instantiate the form using the variable that is automatically defined to point at that form in its unit, ie.
Code:
FrmData
.
Normally to instantiate a new form I would use
Code:
FrmData := TFrmData.Create(Self);

Now ... here is the question.
Can I do this in a generic way for every type of form I have?
Code:
procedure CreateMyForm(TheFrm: TForm);
begin
   TheFrm := ???TheFrm???.Create(Self);
end;

and then call this procedure like this ...

Code:
CreateMyForm(FrmData);

Thanks for your help. [pc3]
I would make my code a lot nicer if I cound do something like this.

Tim
SNL Computing
 
The first is from the borland website:

You just need to register the form classes in the create method of a form you know will be created, or in the initialization section of a library that you use.

procedure TForm1.FormCreate(Sender: TObject);
begin
RegisterClass(Tform2);
RegisterClass(Tform3);
RegisterClass(Tform4);
RegisterClass(Tform5);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
f : Tformclass;
begin
f := tformclass(findClass(edit1.text));
with f.create(self) do
show;
end;
 
If you have access to the form class definitions (via uses) then you can do it this way:

procedure TMyMainForm.LaunchForm(FormClass:TFormClass);
begin
with FormClass.Create(self) do
Show;
end;

procedure TMyMainForm.ShowForms;
begin
LaunchForm(TForm1);
LaunchForm(TForm2);
LaunchForm(TForm3);
end;
--
Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top