When I make a dynamic component from unit I have no problem creating the OnClick event. When I make a dynamic component from unit 2 I am unable to access the OnClick event.
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.