假设我现定义了若干个类似为aForm,bForm,cForm......的Form名称,并把他们存放于数据库的一个字段中,当从数据库中取出到DBGrid中时我可以通过双击某行用下面的语句创建窗体:
aForm:=TaForm.Create(self);
aForm.showmodal;
aForm.free;
我想动态的传递窗体名称,但我不知道窗体类该如何得到(TaForm),请问我这个函数应该怎么写。
aForm:=TaForm.Create(self);
aForm.showmodal;
aForm.free;
我想动态的传递窗体名称,但我不知道窗体类该如何得到(TaForm),请问我这个函数应该怎么写。
解决方案 »
- 轻量级数据库的选择和分析
- 怎样可以实现vclskin皮肤更新,我想在程序里控制它.可以吗 ?
- 如何才能控制打印不走纸,实现即打即停,???
- 如何将记录类型转化为C语言的结构体
- 请教:通过子窗口中的变量改变主窗口状态条的text值后,为什么看不到变化?
- 我知道这个问题比较简单,可我不会啊:如何在两个窗体之间传递值?
- 在程序中用事务操作数据有什么好处?如果我用的数据库是SQLSERVER,不用事务难道就不支持并发了?
- DELPHI怎么复制到当前管理员账户的启动文件夹?
- query.sql.add('and join_date>='+pick1.date);運行時的錯誤是:[error] 'string' and 'tdate'
- 哪里可以找到ADO使用的相关资料
- 怎样改变其他程序的菜单
- delphi程序吃内存,简直疯了!大家快来帮忙阿,散分!
This example creates a handy button component that can be used to automatically create and show forms. It uses properties and events to set the form name and to show the form modally.We're essentially using the same approach that Delphi uses in the Application.CreateForm method. If you have the VCL source you might want to check it out.To dynamically create an object using a text string of its class name, you need to first obtain an object-type reference. This can be done using the FindClass or GetClass functions. These return a TPersistentClass type. To create forms on the fly, you need to recast this as a TFormClass.Once you have this class reference you can use Application.CreateForm or the Create method to create a new object, for example:var
FormName: string;
AForm: TForm;
begin
Application.CreateForm(TFormClass(FindClass('T'+FormName)), AForm);
end;
There is one catch to using FindClass. Before your class can be found it needs to be registered with Delphi, otherwise you get a "Class ... not found" error. The best place to register the class is in the unit that defines the class. Include the following code in the initialization section at the end of the unit: initialization
RegisterClass(TForm2);
end.
var
AClass: TPersistentClass;
AFormClass: TFormClass;
begin
inherited Click;
if FFormName = '' then Exit;
if FForm = nil then
begin
AClass := FindClass('T' + FFormName);
AFormClass := TFormClass(AClass);
FForm := AFormClass.Create(Self);
end;
if FShowModal then
FFormModalResult := FForm.ShowModal
else
FForm.Show;
if Assigned(FAfterShow) then FAfterShow(Self);
end;
You can see we've also added some properties and code to show the form either modal or non-modal, and to execute an event after showing the form. This event is essential for a modal form where you want to do something after the form is hidden or destroyed.The full component source includes additional runtime properties and methods to access the form.http://www.obsof.com/delphi_tips/delphi_tips.html