Solved

Dynamic Form Creation

Posted on 1997-05-11
2
176 Views
Last Modified: 2010-05-18
Hiya,

Is it possible to instantiate objects (specifically a TForm) dynamically
at run-time from a String with value := to the name of a specific form?
I'm assuming it's not possible to store the Form object itself in a
Paradox table but I could store the name of the Form as a string, no?

Any little code snippet would be appreciated.

Thanks,
 Bob Skinner

rskinner@ix.netcom.com
0
Comment
Question by:skin444
2 Comments
 
LVL 3

Accepted Solution

by:
sperling earned 50 total points
ID: 1336127
It is possible to create a form based on the name of the class.

Form1 is of class TForm1
FormMain is of class TForm2

This requires you to add code to register the forms in the initialization section of the units. The following code works under Delphi 3, I'm not sure whether D2 will accept it.

----------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  fc      : TPersistentClass;
  f       : TForm;
begin
  fc := GetClass('TForm2');
  if fc <> nil then begin
    f :=  TFormClass(fc).Create(Application);
    f.Show;
  end;
end;

initialization
  RegisterClass (TForm1);
end.
------------------------------------
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm2 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

initialization
  RegisterClass (TForm2);
end.
-----------------------



0
 

Author Comment

by:skin444
ID: 1336128

Thanks sperling!

It does work under D2.  Code sample was just what I was looking for.

-Bob
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now