delphi - 在运行时构建按钮

标签 delphi

我已经查看了示例,但由于错误而无法编译。

unit Unit2;

interface

uses
  Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,     Vcl.StdCtrls;

type
  TForm2 = class(TForm)
editType: TEdit;
  private
{ Private declarations }
  public
{ Public declarations }
  end;

var
  Form2: TForm2;
  RunTimeButton : TButton;

implementation

{$R *.dfm}

begin
  {Self refers to the form}
  RunTimeButton := TButton.Create(Self);
  {Assign properties now}
  RunTimeButton.Caption := 'Run-time';
  RunTimeButton.Left := 20;
  {Show the button}
  RunTimeButton.Visible := True;
end;

end.

错误是:

[dcc32 Error] Unit2.pas(28): E2003 Undeclared identifier: 'Self'
[dcc32 Error] Unit2.pas(34): E2029 '.' expected but ';' found

知道如何解决吗?我已经查找了错误,但没有结果。

最佳答案

您尚未声明内部执行代码的方法。在对象检查器中找到表单的 OnCreate 事件,双击它并将代码放入 IDE 生成的事件处理程序 stub 中。

您还必须为按钮分配一个Parent。如果按钮成为表单类的成员会好得多。

type
  TForm2 = class(TForm)
    editType: TEdit;
    procedure FormCreate(Sender: TObject);
  private
    FRunTimeButton: TButton;
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
  FRunTimeButton := TButton.Create(Self);
  FRunTimeButton.Parent := Self;
  FRunTimeButton.Caption := 'Run-time';
  FRunTimeButton.Left := 20;
end;

关于delphi - 在运行时构建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31132352/

相关文章:

delphi - 服务处的 LogonUser + CreateProcessAsUser = 错误 1314

delphi - 为什么我的滚动条并不总是正确绘制?

delphi - 如何在Delphi组件中存储大文本?

delphi - Pascal 中的 '#"符号是什么?

c# - 如何使用 Delphi 或 C# 在 Hyper-v 上获取虚拟机状态

delphi - 在 Delphi 中查看图像

Delphi 奇怪的不可访问值(访问冲突)o.O

delphi - Delphi中卡方分布函数的代码

delphi - 在哪里可以找到免费/开源的 Delphi 多媒体组件?

delphi - 带位图的简单组合框