delphi 字符串 左边_从字符串创建Delphi表单

delphi 字符串 左边

There may be instances when you do not know the exact class type of a form object. You may only have the string variable carrying the name of the form's class, such as “TMyForm”.

在某些情况下,您可能不知道表单对象的确切类类型 。 您只能使用带有表单类名称的字符串变量,例如“ TMyForm”。

Note that the Application.CreateForm() procedure expects a variable of type TFormClass for its first parameter. If you can provide a TFormClass type variable (from a string), you will be able to create a form from its name.

请注意,Application.CreateForm()过程的第一个参数期望使用类型为TFormClass的变量。 如果可以提供TFormClass类型变量(从字符串),则可以从其名称创建表单。

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, cast it to TFormClass, and a new TForm object will be created.

FindClass() Delphi函数从string中查找类类型 。 搜索将遍历所有注册的类。 要注册一个类,可以发出一个RegisterClass()过程。 当FindClass函数返回TPersistentClass值时,将其强制转换为TFormClass,然后将创建一个新的TForm对象。

样例练习 ( Sample Exercise )

  1. Create a new Delphi project and name the main form: MainForm (TMainForm).

    创建一个新的Delphi项目并命名主窗体:MainForm(TMainForm)。

  2. Add three new forms to the project, name them:

    将三个新表单添加到项目中,命名为:
  3. FirstForm (TFirstForm)

    FirstForm(TFirstForm)
  4. SecondForm (TSecondForm)

    SecondForm(TSecondForm)
  5. ThirdForm (TThirdForm)

    ThirdForm(TThirdForm)
  6. Remove the three new forms from the "Auto-create Forms" list in the Project-Options dialog.

    从“项目选项”对话框的“自动创建表单”列表中删除三个新表单。
  7. Drop a ListBox on the MainForm and add three strings: 'TFirstForm', 'TSecondForm', and 'TThirdForm'. 

    在MainForm上放置一个ListBox并添加三个字符串:“ TFirstForm”,“ TSecondForm”和“ TThirdForm”。

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

In the MainForm's OnCreate event register the classes:

在MainForm的OnCreate事件中注册类:


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

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

单击按钮后,找到所选表单的类型名称,并调用自定义CreateFormFromName过程:


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

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.

如果在列表框中选择了第一项,则“ s”变量将保存“ TFirstForm”字符串值。 CreateFormFromName将创建TFirstForm表单的实例。

翻译自: https://www.thoughtco.com/create-delphi-form-from-a-string-1057672

delphi 字符串 左边

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值