我已经查看了示例,但由于错误而无法编译。
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/