Solved

Dynamic Form Creation

Posted on 1997-05-11
2
179 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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