Creating a form based on a string

Written by Embarcadero USA on Posted in PROGRAMMING

 Technical Information Database

TI1502D.txt   Creating a form based on a string
Category   :Application Interop
Platform    :All
Product    :Delphi  All

Description:
Creating a form based on a string.

OVERVIEW

This document demonstrates how to instantiate a Delphi form
based on a string which specifies the name of the type. 
Sample code is given.

WHO SHOULD USE THIS DOCUMENT

Anyone with a basic familiarity with Delphi programming.
Applies to any version of Delphi.

INSTANTIATING A FORM BASED ON A STRING

To be able to instantiate forms based on the string
representing the names of their type, you must first
register the type with Delphi.  This is accomplished
with the function "RegisterClass".
RegisterClass is prototyped as follows:

procedure RegisterClass(AClass: TPersistentClass);

AClass is a class of TPersistent. In other words, the class
you are registering must be descended at some point from
TPersistent.  Since all Delphi controls, including 
forms, fit this requiremnet, we have will have no problem.
This could not be used, for instance, to register classes 
descended directly from TObject.

Once the class has been registered, you can find a pointer
to the type by passing a string to FindClass.  This will
return a class reference, which you can use to create the
form. 
An example is in order:


procedure TForm1.Button2Click(Sender: TObject);
var
  b : TForm;
  f : TFormClass;
begin
  f := TFormClass(findClass('Tform2'));
  b := f.create(self);
  b.show;
end;

This will create the TForm2 type that we registered
with RegisterClass.


WORKING SAMPLE PROGRAM


Create a new project, and then add 4 more forms, for
a total of 5. You can populate them with controls if
you like, but for this example, it is not important.

On the first form, put down an edit control, and
a pushbutton.  Take all forms, but the main form,
out of the AutoCreate List.
Finally, paste the following code over the code 
in unit1, this will give you the ability to type
the Class NAME into the Edit, and it will create
that form for you.


unit Unit1;

interface

uses
  Unit2, Unit3, Unit4, Unit5, Windows, Messages,
  SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

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;


Reference:


7/16/98 4:34:14 PM
 


Article originally contributed by

Tags: Delphi



Check out more tips and tricks in this development video: