Delphi OnClick 多个单元的问题

标签 delphi event-handling onclick

当我从单元创建动态组件时,创建 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/

相关文章:

Delphi XE - TObjectList 排序

java - 我的鼠标处理程序类导致鼠标移动和结果之间呈几何级数,我正在寻找原因

Jquery 在页面加载时而不是 onclick

javascript - 不使用 jQuery 获取文本字段的当前值

javascript - 我不知道如何让我的 iFrame 在点击时下拉

delphi - 如何销毁运行时定义的 TClientDataSet TFields?

delphi - 如何检查Application.MainForm是否有效?

java - MouseDragged 和 MouseMoved 在 Java Applet 中不起作用

javascript - 如何在 mouseenter 事件后定位特定元素?

c++ - 如何使用 TIdHTTPProxyServer 重定向 Post 请求