OOP Delphi

Title: How do I assign a method to the event of a dynamically created object?
Question: Event handlers can be also created at runtime. This easy example shows how to do this. It also shows the differenz between the Owner and the Parent of the VCL. This you need when you dynamically create Visual Controls
Answer:
unit Unit1;
interface
uses SysUtils, WinTypes, WinProcs, Messages, Classes,
Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
end;
var Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
hBtn: TButton;
begin
hBtn := TButton.create(Application);
hBtn.parent := Form1;
hBtn.caption := 'New Button';
hBtn.OnClick := Button2.OnClick;
hBtn.show;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage((Sender as TButton).Caption);
end;
end.