Delphi - 引用在运行时创建的对象时遇到问题

Delphi - trouble referencing objects created at runtime

我正在使用 Delphi 7. 我已经编写了一些代码来在运行时创建按钮(我需要在每个表单的完全相同的位置放置大量完全相同的按钮,这就是为什么我我决定这样做)。但是我在程序中引用它们时遇到了麻烦(准确地说是 OnClick)。我希望在单击按钮时打开另一个表单。

unit Unit2;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls, jpeg, ExtCtrls;

procedure buttons(a: TForm);

type
 TForm2 = class(TForm)
  Image1: TImage;
  procedure FormShow(Sender: TObject);
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
  private
   { Private declarations }
  public
   { Public declarations }
end;

var
 Form2: TForm2; Button1, Button2, Button3, Button4: TButton;

implementation

uses Unit3, Unit4;

{$R *.dfm}

procedure buttons(a: TForm);
 begin
  Button1 := TButton.Create(a);
  Button1.Name := 'Button1';
  Button1.Left := 712;
  Button1.Top := 96;
  Button1.Width := 81;
  Button1.Height := 41;
  Button1.Visible := True;
  Button1.Parent := a;
  Button1.Enabled := False;
  Button1.Caption := 'Go forwards';
  Button2 := TButton.Create(a);
  Button2.Name := 'Button2';
  Button2.Left := 800;
  Button2.Top := 152;
  Button2.Width := 81;
  Button2.Height := 41;
  Button2.Visible := True;
  Button2.Parent := a;
  Button2.Enabled := False;
  Button2.Caption := 'Go right';
  Button3 := TButton.Create(a);
  Button3.Name := 'Button3';
  Button3.Left := 624;
  Button3.Top := 152;
  Button3.Width := 81;
  Button3.Height := 41;
  Button3.Visible := True;
  Button3.Parent := a;
  Button3.Enabled := False;
  Button3.Caption := 'Go left';
  Button4 := TButton.Create(a);
  Button4.Name := 'Button4';
  Button4.Left := 712;
  Button4.Top := 208;
  Button4.Width := 81;
  Button4.Height := 41;
  Button4.Visible := True;
  Button4.Parent := a;
  Button4.Enabled := False;
  Button4.Caption := 'Go back';
 end;


procedure TForm2.FormShow(Sender: TObject);
 begin
  buttons(Form2);
  Button1.Enabled := True;
  Button2.Enabled := True;
 end;

procedure TForm2.Button1Click(Sender: TObject);
 begin
  Form3.Show;
  Form2.Hide;
 end;

procedure TForm2.Button2Click(Sender: TObject);
 begin
  Form4.Show;
  Form2.Hide;
 end;

end.

我也在 'type' 中声明了 OnClicks,我可能应该这样做。该程序运行,但创建的按钮不起作用,但可以单击。想法?

P.S.: 我知道我可以编写更紧凑的代码来创建所有这些按钮,但我没有时间考虑它,而且它几乎没有意义。我知道它可能难以阅读 - 你只需要知道,我在每个按钮上设置了相同类型的属性 - 你只需要查看 Button1,其他的都是相同的。

P.P.S.: 这不是一个重复的问题:Delphi - Referencing Components created at Runtime。我找不到那个问题的解决方案。

首先你应该清理一下你的代码。但这不是您的代码无法正常工作的原因。这是因为您忘记为按钮分配 OnClick 事件:

看看这个:

unit Unit19;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm19 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    Button1: TButton;
    Button2: TButton;
    Procedure CreateButtons;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form19: TForm19;

implementation

{$R *.dfm}

procedure TForm19.Button1Click(Sender: TObject);
begin
  Caption := 'Button1 Clicked';
end;

procedure TForm19.Button2Click(Sender: TObject);
begin
  Caption := 'Button2 Clicked';
end;

procedure TForm19.CreateButtons;
begin
  Button1 := TButton.Create(Self);
  Button1.Name := 'Button1';
  Button1.Left := 712;
  Button1.Top := 96;
  Button1.Width := 81;
  Button1.Height := 41;
  Button1.Visible := True;
  Button1.Parent := Self;
  Button1.Enabled := False;
  Button1.OnClick := Button1Click;

  Button1.Caption := 'Go forwards';
  Button2 := TButton.Create(Self);
  Button2.Name := 'Button2';
  Button2.Left := 800;
  Button2.Top := 152;
  Button2.Width := 81;
  Button2.Height := 41;
  Button2.Visible := True;
  Button2.Parent := Self;
  Button2.Enabled := False;
  Button2.Caption := 'Go right';
  Button2.OnClick := Button2Click;
end;

procedure TForm19.FormCreate(Sender: TObject);
begin
  CreateButtons;
end;

end.

首先是清理:我已将您的按钮声明移至拥有它们的表单的私有部分。

关于按钮的所有者,构造函数的参数;它必须是表格。因为当你销毁表单时,它也会销毁你的按钮,并且不会泄漏任何内存。

然后用此行解决的缺失 OnClick 事件:

Button1.OnClick := Button1Click;

我只是告诉按钮当用户点击按钮时调用哪个过程。

我希望这能回答您的问题。

在你的情况下,我会使用 Frames。您可以将所有按钮放在该框架上,您可以使用属性更改行为,分配所有需要的事件并在设计时或 运行 时间

将其放在您的窗体上