如何使用Delphi中的几个组件以编程方式创建表单

Mik*_*ail 10 delphi delphi-7

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

unit clsTStudentInfoForm;

interface

    uses Forms;

    type
        TStudentInfoForm = class (TForm)

        end;

implementation


end.
Run Code Online (Sandbox Code Playgroud)

我的主表单上还有一个按钮(这只是一个常规表单,应该在运行时创建并显示上面的表单),点击它时会创建并显示学生表单作为模态窗口.它确实显示了表格,但没有任何内容.您唯一能做的就是单击窗口右上角的关闭按钮将其关闭.

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

在此输入图像描述

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

任何建议和示例将受到高度赞赏.谢谢.

NGL*_*GLN 19

默认情况下(即:使用所有默认IDE配置设置),将自动创建新设计的表单.只显示主表单,辅助表单可以显示为:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show;
  Form3.ShowModal;
end;
Run Code Online (Sandbox Code Playgroud)

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

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

procedure TForm1.Button1Click(Sender: TObject);
var
  Form: TForm2;
begin
  Form := TForm2.Create(Self);
  Form.Show;
end;
Run Code Online (Sandbox Code Playgroud)

这也说明了不需要二级表单的全局变量,并且通常的做法是尽快删除它们以防止错误使用:

type
  TForm2 = class(TForm)
  end;

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

implementation
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

如您所见,我使用了CreateNew构造函数.这对衍生品来说是必要T(Custom)Form:

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

对于所有其他容器控件(如TPanel,TFrame等),您可以覆盖默认的构造函数Create.

请将此表格称为如下:

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

要么:

procedure TForm1.Button1Click(Sender: TObject);
begin
  FForm := TForm2.CreateNew(Application);
  FForm.Show;
end;
Run Code Online (Sandbox Code Playgroud)

在最后一种情况下,表单不会被释放,但在关闭时会被隐藏,因此您需要将其引用存储在私有字段(FForm)中并稍后释放.或者你可以自动完成:

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.
Run Code Online (Sandbox Code Playgroud)

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

procedure TForm1.Button1Click(Sender: TObject);
begin
  TForm2.CreateNew(Self).Show;
end;
Run Code Online (Sandbox Code Playgroud)

无论你通过了,Self,Applicationnil作为业主为新的形式取决于当你想要得到它自动销毁的情况下对你没有手动或通过释放它OnClose的事件.运用

  • Self:将在销毁调用表单时销毁新表单.当调用表单不是主表单时,这尤其有用.
  • Application:将在应用程序结束时销毁新表单.这将是我的首选.
  • nil:不会破坏新表单并导致应用程序完成时内存泄漏.但是,当Windows终止进程时,内存最终会被释放.


And*_*and 7

使用动态控件创建模态表单很容易:

procedure CreateGreetingForm;
var
  frm: TForm;
  lbl: TLabel;
  edt: TEdit;
  btn: TButton;
begin

  frm := TForm.Create(nil);
  try
    lbl := TLabel.Create(frm);
    edt := TEdit.Create(frm);
    btn := TButton.Create(frm);
    frm.BorderStyle := bsDialog;
    frm.Caption := 'Welcome';
    frm.Width := 300;
    frm.Position := poScreenCenter;

    lbl.Parent := frm;
    lbl.Top := 8;
    lbl.Left := 8;
    lbl.Caption := 'Please enter your name:';

    edt.Parent := frm;
    edt.Top := lbl.Top + lbl.Height + 8;
    edt.Left := 8;
    edt.Width := 200;

    btn.Parent := frm;
    btn.Caption := 'OK';
    btn.Default := true;
    btn.ModalResult := mrOk;
    btn.Top := edt.Top + edt.Height + 8;
    btn.Left := edt.Left + edt.Width - btn.Width;

    frm.ClientHeight := btn.Top + btn.Height + 8;
    frm.ClientWidth := edt.Left + edt.Width + 8;

    if frm.ShowModal = mrOk then
      ShowMessageFmt('Welcome, %s', [edt.Text]);

  finally
    frm.Free;
  end;

end;
Run Code Online (Sandbox Code Playgroud)

  • @Mikhail:然后在“TStudentInfoForm”的“FormCreate”中执行此操作。但是如果你有一个“表单类文件”,你可以直观地布局它,而不需要首先在代码中进行。 (2认同)
  • @Remy:这不是我的观点,显然你没有阅读。:-) 如果您想将控件放在表单的声明中,您也可以使用默认的 .dfm 并以可视方式创建它,您可以在其中排列它并连接事件处理程序并设置属性。但你是对的 - 在正常情况下构造函数是正确的。如果您只是无缘无故地执行一次,那么“FormCreate”就可以了,因为没有其他人会创建该表单。这并不像您在创建一个可重用的组件;而是在创建一个组件。无需为其他开发商预订活动。 (2认同)