Here"s how to create a Delphi form from a string.
There may be instances when you do not know the exact class type of a form object. You may only have the string variable carrying the name of the form's class, such as “TMyForm”.
Note that the Application.CreateForm() procedure expects a variable of type TFormClass for its first parameter. If you can provide a TFormClass type variable (from a string), you will be able to create a form from its name.
The FindClass() Delphi function locates a class type from a string. The search goes through all registered classes. To register a class, a procedure RegisterClass() can be issued. When the FindClass function returns a TPersistentClass value, cast it to TFormClass, and a new TForm object will be created.
A sample exercise
procedure TMainForm.FormCreate(Sender: TObject);beginRegisterClass(TFirstForm);RegisterClass(TSecondForm);RegisterClass(TThirdForm);end;
In the MainForm's OnCreate event register the classes:
procedure TMainForm.CreateFormButtonClick(Sender: TObject);vars : string;begins := ListBox1.Items[ListBox1.ItemIndex];CreateFormFromName(s);end;
Once the button is clicked, find the selected form's type name, and call a custom CreateFormFromName procedure:
procedure CreateFormFromName(const FormName : string);varfc : TFormClass;f : TForm;beginfc := TFormClass(FindClass(FormName));f := fc.Create(Application);f.Show;end; (* CreateFormFromName *)
If the first item is selected in the list box, the "s" variable will hold the "TFirstForm" string value. The CreateFormFromName will create an instance of the TFirstForm form.
Congratulations! You have created a Delphi form from a string!
Note that the Application.CreateForm() procedure expects a variable of type TFormClass for its first parameter. If you can provide a TFormClass type variable (from a string), you will be able to create a form from its name.
The FindClass() Delphi function locates a class type from a string. The search goes through all registered classes. To register a class, a procedure RegisterClass() can be issued. When the FindClass function returns a TPersistentClass value, cast it to TFormClass, and a new TForm object will be created.
A sample exercise
- Create a new Delphi project and name the main form: MainForm (TMainForm).
- Add three new forms to the project, name them:
- FirstForm (TFirstForm)
- SecondForm (TSecondForm)
- ThirdForm (TThirdForm)
- Remove the three new forms from the "Auto-create Forms" list in the Project-Options dialog.
- Drop a ListBox on the MainForm and add three strings: 'TFirstForm', 'TSecondForm', and 'TThirdForm'.
procedure TMainForm.FormCreate(Sender: TObject);beginRegisterClass(TFirstForm);RegisterClass(TSecondForm);RegisterClass(TThirdForm);end;
In the MainForm's OnCreate event register the classes:
procedure TMainForm.CreateFormButtonClick(Sender: TObject);vars : string;begins := ListBox1.Items[ListBox1.ItemIndex];CreateFormFromName(s);end;
Once the button is clicked, find the selected form's type name, and call a custom CreateFormFromName procedure:
procedure CreateFormFromName(const FormName : string);varfc : TFormClass;f : TForm;beginfc := TFormClass(FindClass(FormName));f := fc.Create(Application);f.Show;end; (* CreateFormFromName *)
If the first item is selected in the list box, the "s" variable will hold the "TFirstForm" string value. The CreateFormFromName will create an instance of the TFirstForm form.
Congratulations! You have created a Delphi form from a string!