gpt4 book ai didi

Delphi OnClick 多个单元的问题

转载 作者:行者123 更新时间:2023-12-03 15:55:36 25 4
gpt4 key购买 nike

当我从单元创建动态组件时,创建 OnClick 事件没有问题。当我从单元 2 制作动态组件时,我无法访问 OnClick 事件。

unit Unit1  
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
Procedure ClickBtn1(Sender: TObject);
end;

var
Form1: TForm1;
MyBtn1: TButton;
implementation

{$R *.dfm}

{ TForm1 }
uses Unit2;

procedure TForm1.ClickBtn1;
begin
MyBtn1.Caption := 'OK';
MakeBtn2;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
MyBtn1 := TButton.Create(Self);
MyBtn1.Parent := Form1;
MyBtn1.Name := 'Btn1';
MyBtn1.Left := 50;
MyBtn1.Top := 100;
MyBtn1.Caption := 'Click Me';
MyBtn1.OnClick := ClickBtn1;
end;


end.



unit Unit2;

interface

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

procedure MakeBtn2;
procedure ClickBtn2;
var
MyBtn2: TButton;

implementation

Uses Unit1;

procedure MakeBtn2;
begin
MyBtn2 := TButton.Create(Form1);
MyBtn2.Parent := Form1;
MyBtn2.Name := 'Btn2';
MyBtn2.Left := 250;
MyBtn2.Top := 100;
MyBtn2.Caption := 'Click Me';
MyBtn2.OnClick := ClickBtn2; //Compiler ERROR
end;

procedure ClickBtn2;
begin
MyBtn1.Caption := 'OK';
end;
end.

最佳答案

看看这篇文章“Creating A Component at Runtime

引用:

I want to create a button in code, put it on a form and attach a procedure to its click event. How can I get the click event linked to a predefined procedure name from code? I assume the IDE linking in the object browser is key to the answer, but I want to do this at run time, not in development.

First of all, you can assign any object's method to another method as long as it has the same form. Look at the code below:

{This method is from another button that when pressed will create
the new button.}
procedure TForm1.Button1Click(Sender: TObject);
var
btnRunTime : TButton;
begin
btnRunTime := TButton.Create(form1);
with btnRunTime do begin
Visible := true;
Top := 64;
Left := 200;
Width := 75;
Caption := 'Press Me';
Name := 'MyNewButton';
Parent := Form1;
OnClick := ClickMe;
end;
end;

{This is the method that gets assigned to the new button's OnClick method}
procedure TForm1.ClickMe(Sender : TObject);
begin
with (Sender as TButton) do
ShowMessage('You clicked me');
end;

As you can see, I created a new method called ClickMe, which was declared in the private section of Form1:

type
TForm1 = class(TForm
...
...
private
procedure ClickMe(Sender : TObject);
published
end;

有关其他示例和说明,另请参阅以下内容:

关于Delphi OnClick 多个单元的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1896538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com