在运行时设置Delphi按钮的OnClick过程

Jam*_*son 0 delphi runtime onclick button

我有一个程序,我需要在其中更新数据库表,并在编辑框中输入信息,最后用一个按钮进行更新.但是,表单是在运行时创建的,包括按钮在内的所有元素也以相同的方式创建.我想办法允许数据库参数定义更新数据库的过程,例如:

procedure UpdateDatabase(Field1,Field2,Field3:string);
begin
//update database here...
end;
Run Code Online (Sandbox Code Playgroud)

然后将我的按钮的OnClick事件分配给此过程,其参数预填充如下:

Button1.OnClick := UpdateDatabase(Edit1.text,Edit2.Text,Edit3.Text);
Run Code Online (Sandbox Code Playgroud)

但是,类型不兼容,因为它需要不同的数据类型.我还注意到参数通常不能传递给OnClick函数.实际上有没有办法实现我提出的建议?

这是我当前的创建按钮代码:

function buttonCreate(onClickEvent: TProcedure; 
  left: integer; top: integer; width: integer; height: integer; 
  anchors: TAnchors; caption: string; parent: TWinControl; form: TForm;): TButton;
var
  theButton: TButton;
begin
  theButton := TButton.Create(form);
  theButton.width := width;
  theButton.height := height;
  theButton.left := left;
  theButton.top := top;
  theButton.parent := parent;
  theButton.anchors := anchors;
  //theButton.OnClick := onClickEvent;
  theButton.Caption := caption;
  result := theButton;
end;
Run Code Online (Sandbox Code Playgroud)

任何和所有帮助表示赞赏!

Jer*_*dge 6

必须准确声明事件处理程序如何定义事件类型.OnClick事件被声明为TNotifyEvent带参数的事件(Sender: TObject).你不能打破这个规则.

在您的情况下,您可以将自己的过程包装在事件处理程序中,如此...

procedure TForm1.Button1Click(Sender: TObject);
begin
  UpdateDatabase(Edit1.text,Edit2.Text,Edit3.Text);
end;
Run Code Online (Sandbox Code Playgroud)

请注意,这TNotifyEvent是一个"对象"过程,这意味着您的事件处理程序必须在对象内声明.在您的情况下,应在表单内声明事件处理程序(而不是在全局位置).