當我從單元創建一個動態組件時,我無法創建OnClick事件。 當我製作一個動態組件單元2我無法訪問OnClick事件。多個單元的Delphi 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.
什麼將acheive我想要的東西的最佳方式? – 2009-12-13 14:13:24
使用正確的簽名在表單上聲明一個方法,並將您要執行的代碼放在那裏。 – 2009-12-13 14:28:21