Código Delphi
[-]unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure ButtonClick(Sender: TObject);
private
public
end;
var
Form1: TForm1;
Button1,Button2: TButton;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1:= TButton.Create(Self);
Button1.Parent := Self;
Button1.Caption := 'Botón 1';
Button1.Left:= 20;
Button1.Top:= 10;
Button1.OnClick := ButtonClick;
Button2:= TButton.Create(Self);
Button2.Parent := Self;
Button2.Caption := 'Botón 2';
Button2.Left:= 100;
Button2.Top:= 10;
Button2.OnClick := ButtonClick;
end;
procedure TForm1.ButtonClick(Sender: TObject);
begin
ShowMessage('Presionaste el ' + (Sender as TButton).Caption + '!');
end;
end.
Y me parece que al no interesar más datos de los botones se podría hacer algo como ésto:
Código Delphi
[-]unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure ButtonClick(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
with TButton.Create(Self) do
begin
Parent := Self;
Caption := 'Botón 1';
Left:= 20;
Top:= 10;
OnClick := ButtonClick;
end;
with TButton.Create(Self) do
begin
Parent := Self;
Caption := 'Botón 2';
Left:= 100;
Top:= 10;
OnClick := ButtonClick;
end;
end;
procedure TForm1.ButtonClick(Sender: TObject);
begin
ShowMessage('Presionaste el ' + (Sender as TButton).Caption + '!');
end;
end.
PD: Claro que hay N formas de obtener lo que quieres.