如何从FORM的名字(Name)创建Form的实例(100分)

  • 主题发起人 主题发起人 foxhu
  • 开始时间 开始时间
F

foxhu

Unregistered / Unconfirmed
GUEST, unregistred user!
我的Project有100个Form,每个Form的名字存于一个数据库中
如:
001, FormA 类为: TFormA
002, FormB 类为: TFormB
我在编程时用户输入001,我可以知道需要显示FormA(以上表格)
但是我如何从Form的名字FormA创建他,并且显示他。
(我的Project启动是没有自动创建)
Application.FindComponent
Application.Components[n]
都没有用,请教各位,送上100分
 
用FindClass,以下是它的说明
function FindClass(const ClassName: string): TPersistentClass;
Call FindClass to locate a class type by name. FindClass searches the classes that the streaming system knows about. Form classes and component classes that are referenced in a form declaration (instance variables) are automatically registered with the streaming system. Other classes can be registered (with a call to RegisterClasses) so that the streaming system will recognize that class in a stream and know how to construct it.
If the requested class name cannot be found, FindClass raises an exception.
 
用FindClass,以下是它的说明
function FindClass(const ClassName: string): TPersistentClass;
Call FindClass to locate a class type by name. FindClass searches
the classes that the streaming system knows about. Form classes and
component classes that are referenced in a form declaration (instance
variables) are automatically registered with the streaming system.
Other classes can be registered (with a call to RegisterClasses) so
that the streaming system will recognize that class in a stream and
know how to construct it.
If the requested class name cannot be found, FindClass raises an
exception
对不起,上一条消息没贴好,重贴一次,并补充一句。
你的那100个FORM必须要先用 RegisterClass 注册.
procedure RegisterClass(AClass: TPersistentClass);

 
component: edit1,button1.
uses Unita,unitb...;
...
procedure TForm1.Button1Click(Sender: TObject);
var formnew:TFORM;
begin
Application.CreateForm(TComponentClass(findClass(Edit1.Text)),formnew);
formnew.show;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
RegisterClasses([tforma,tformb...]);
end;

that 's OK.
 
给你一个比较详细的,是从Inprise站点来的
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

Thisdo
cument 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 THISdo
CUMENT

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, putdo
wn 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;
 
多人接受答案了。
 
后退
顶部