I tried a script from a web site I run http://www.delphi-central.com/runtime.aspx and succeed.
private
{ Private declarations }
procedure CustomButtonClick(Sender: TObject);
procedure TForm1.AddNewButtonClick(Sender: TObject);
var
NewButton : TButton;
begin
NewButton := TButton.create(self);
with NewButton do
begin
Top := 30;
Width := 60;
Left := Width * (self.ControlCount-2);
Parent := self;
OnClick := CustomButtonClick;
Caption := 'Button '+ inttostr (self.ControlCount-2);
end; //With
end;
procedure TForm1.DeleteLastButtonClick(Sender: TObject);
begin
if Self.ControlCount>2 then
TButton (Controls[ControlCount-1]).destroy;
end;
procedure TForm1.CustomButtonClick(Sender: TObject);
begin
ShowMessage(TButton(Sender).caption + ' Pressed');
end;
But if I change the OnClick,
OnClick := CustomButtonClick; ==> OnClick := DeleteLastButtonClick;
it will generate an error message. How could this happen ...???