1. Technology

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/library/weekly/aa080905a.htm

was emailed to:

Thanks for sharing About.com with others!

Creating a Delphi Form from a String
Here's how to create an instance of a Delphi form, from its name. Example: if a string 'TMyForm' is provided, a new form of type TMyForm should be created ...
 More of this Feature
• Download source code
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Manipulating Delphi Forms
• Delphi OOP
• RTTI in Delphi

Suppose you have a bunch of forms in your application (like you probably do). Of course, much of those forms get created dynamically at run time (and are removed from the "Auto-Create Forms" list in the Project properties window).

Let's say you have a form named "MyForm" where the class name is "TMyForm" you want to create. To create an instance of the "MyForm" form, at run-time, you could use the next code (to have it displayed modally, and freed as soon as it gets closed):

var
  myForm : TMyForm;
begin
  myForm := TMyForm.Create(nil);
  try
    myForm.ShowModal;
  finally
    myForm.Free;
  end;
end;

Or, you could set the "Application" to be the owner of the MyForm form, and issue a command like (to have it created and freed at a later time):

var
  myForm : TMyForm;
begin
  Application.CreateForm(TMyForm, myForm);
  my.Show;
end;

Now, what if you *do not* know the exact class type of a form object. What if you only have the string variable carrying the name of the form's class, as 'TMyForm'? You cannot use any of the code samples provided above. Application.CreateForm expects a variable of a TFormClass type NOT a string. You most certainly cannot use 'TmyForm'.Create(...)!

New Form Instance from a String
Note that the Application.CreateForm() procedure expects a variable of type TFormClass for its first parameter. If we can provide a TFormClass type variable (from a string), we'll 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, we cast it to TFormClass, and a new TForm object can be created!

An example
Create a new Delphi project and name the main form: MainForm (TMainForm). Add three new forms to the project, name those: FirstForm (TFirstForm), SecondForm (TSecondForm), ThirdForm (TThirdForm). Remove those from the "Auto-create Forms" list in the Project-Options dialog. Drop a ListBox on the MainForm and add 3 strings ('TFirstForm', 'TSecondForm', 'TThirdForm') to it:

An instance of a form, from string...

In the MainForm's OnCreate event register the classes:

procedure TMainForm.FormCreate(
  Sender: TObject);
begin
  RegisterClass(TFirstForm);
  RegisterClass(TSecondForm);
  RegisterClass(TThirdForm);
end;

Once the button is clicked, we find the selected form's type name, and call a custom CreateFormFromName procedure:

procedure TMainForm.CreateFormButtonClick(
  Sender: TObject);
var
  s : string;
begin
  s := ListBox1.Items[ListBox1.ItemIndex];
  CreateFormFromName(s);
end;

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:

procedure CreateFormFromName(
  const FormName : string);
var
  fc : TFormClass;
  f : TForm;
begin
  fc := TFormClass(FindClass(FormName));
  f := fc.Create(Application);
  f.Show;
end; (* CreateFormFromName *)

That's it!

Creating a form from its class name...

If you need any help with the code, be sure to post your questions on the Delphi Programming Forum.

©2014 About.com. All rights reserved.