• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用Delphi的几个组件在程序创建表单

用户头像
it1352
帮助1

问题说明

我正在使用Delphi 7,我正在以编程方式创建一个表单。这是我的表单类存根:

I'm working with Delphi 7 and I'm trying to create a form programmatically. Here's my form class stub:

unit clsTStudentInfoForm;

interface

    uses Forms;

    type
        TStudentInfoForm = class (TForm)

        end;

implementation


end.

我的主窗体上还有一个按钮(这只是一个应该创建和显示的常规表单在运行时的上面的形式),并且当它被单击时,其创建并将学生表单显示为模态窗口。它显示的形式,但没有什么。您唯一可以做的是点击窗口右上角的关闭按钮关闭它。

I also have a button on my main form (that's just a regular form that's supposed to create and show the form above at run-time) and when clicked it creates and shows the student form as a modal window. It does show the form but there's nothing on it. The only thing you can do is click the close button at the upper-right corner of the window to close it.

procedure TLibraryForm.btnShowStudentIfoFormClick(Sender: TObject);
var
    f : TStudentInfoForm;
begin
    f := TStudentInfoForm.CreateNew(Self);
    f.ShowModal;
    f.Free;
    f := nil;
end;

我不知道如何添加组件到一个编程创建的表单(不是在运行时,而是到源代码)。你可以帮我写一些代码,将一个Okay按钮添加到学生表单中,并设置标题和表单的高度和宽度(代码必须写在学生表单文件中)?

I have no idea how to add components to a programmatically-created form (not at run-time, but to the source code). Can you help me write some code that adds an Okay button to the student form as well as sets the caption and the form's height and width (the code has to be written in the student form file)?

将非常感谢任何建议和示例。谢谢。

Any suggestions and examples will be highly appreciated. Thank you.

正确答案

#1

默认情况下(即所有默认IDE配置设置),新设计的表单将自动创建。只能显示主窗体,次级窗体可以显示:

By default (that is: with all default IDE configuration settings), newly designed forms are automatically created. Only the main form will be shown, and secondary forms can be shown with:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show;
  Form3.ShowModal;
end;

禁用此自动创建选项是很常见的做法。转到:工具>(环境)选项>(VCL)设计器>模块创建选项,并禁用/取消选中自动创建表单&数据模块选项。

It is good common practice to disable this auto creation option. Go to: Tools > (Environment) Options > (VCL) Designer > Module creation options, and disable/uncheck the Auto create forms & data modules option.

相反,只有在需要时才创建(已设计)的表单:

Instead, create an (already designed) form only when it is needed:

procedure TForm1.Button1Click(Sender: TObject);
var
  Form: TForm2;
begin
  Form := TForm2.Create(Self);
  Form.Show;
end;

这也说明了不需要辅助表单的全局变量,这是很常见的尽量删除它们以防止错误使用:

This illustrates as well that the global variables for the secondary forms are not needed, and it is good common practice to delete them as soon as possible to prevent wrong usage:

type
  TForm2 = class(TForm)
  end;

//var
//  Form2: TForm2;  << Always delete these global variable

implementation






如果您不想使用表单设计器设置此类辅助表单,则需要在运行时在代码中创建所有控件。如下:


If you do not want to set up such secondary form with the form designer, then you need to create all controls in code at runtime. As follows:

unit Unit2;

interface

uses
  Classes, Forms, StdCtrls;

type
  TForm2 = class(TForm)
  private
    FButton: TButton;
  public
    constructor CreateNew(AOwner: TComponent; Dummy: Integer = 0); override;
  end;

implementation

{ TForm2 }

constructor TForm2.CreateNew(AOwner: TComponent; Dummy: Integer = 0);
begin
  inherited CreateNew(AOwner);
  FButton := TButton.Create(Self);
  FButton.SetBounds(10, 10, 60, 24);
  FButton.Caption := 'OK';
  FButton.Parent := Self;
end;

end.

如你所见,我使用了 CreateNew 构造函数。这是 T(自定义)表单<必要 / code>衍生物:

As you see, I used the CreateNew constructor. This is necessary for T(Custom)Form derivatives:


使用 CreateNew 而不是创建来创建表单,而不使用关联的.DFM文件进行初始化。如果 TCustomForm 后代不是 TForm ,请始终使用 CreateNew 对象或后代 TForm

Use CreateNew instead of Create to create a form without using the associated .DFM file to initialize it. Always use CreateNew if the TCustomForm descendant is not a TForm object or a descendant of TForm.

对于所有其他容器控件作为 TPanel TFrame 等),您可以覆盖默认构造函数创建

For all other container controls (such as TPanel, TFrame, etc.) you can override the default constructor Create.

调用此表单如下:

procedure TForm1.Button1Click(Sender: TObject);
var
  Form: TForm2;
begin
  Form := TForm2.Create(nil);
  try
    Form.ShowModal;
  finally
    Form.Free;
  end;
end;

或:

procedure TForm1.Button1Click(Sender: TObject);
begin
  FForm := TForm2.CreateNew(Application);
  FForm.Show;
end;

在最后一种情况下,表单不被释放,但在关闭时隐藏,因此您需要将其引用存储在私有字段( FForm )中,然后释放它。或者您可以自动执行:

In this last case, the form is not freed but hidden when it is closed, so you need to store its reference in a private field (FForm) and free it later. Or you can do it automatically:

unit Unit2;

interface

uses
  Classes, Forms, StdCtrls;

type
  TForm2 = class(TForm)
  private
    FButton: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  public
    constructor CreateNew(AOwner: TComponent; Dummy: Integer = 0); override;
  end;

implementation

{ TForm2 }

constructor TForm2.CreateNew(AOwner: TComponent; Dummy: Integer = 0);
begin
  inherited CreateNew(AOwner);
  OnClose := FormClose;
  FButton := TButton.Create(Self);
  FButton.SetBounds(10, 10, 60, 24);
  FButton.Caption := 'OK';
  FButton.Parent := Self;
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

end.

现在,您可以在不存储参考的情况下调用它:

Now, you could call it without storing a reference:

procedure TForm1.Button1Click(Sender: TObject);
begin
  TForm2.CreateNew(Self).Show;
end;






无论您是否通过自己的应用程序 nil 作为新窗体的所有者取决于您何时如果您不手动或通过 OnClose 事件释放它,则自动销毁。使用


Whether you pass, Self, Application or nil as owner for the new form depends on when you want to get it automatically destroyed in case you do not free it manually or via the OnClose event. Using

    • Self :当调用表单被销毁时,将会销毁新表单。当调用表单不是主窗体时,这是特别有用的。
    • 应用程序:将在应用程序结束时销毁新窗体。这将是我的首选。
    • nil :不会破坏新的表单,并在应用程序完成时导致内存泄漏。尽管如此,当Windows杀死该进程时,内存将最终被释放。


  • Self: will destroy the new form when the calling form is destroyed. This is particularly usefull when the calling form isn't the main form.
  • Application: will destroy the new form when the application ends. This would be my preferred choice.
  • nil: will not destroy the new form and results in a memory leak at application's finish. Though, memory will ultimately be released when Windows kills the process.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfiaja
系列文章
更多 icon
同类精品
更多 icon
继续加载