Delphi 动态创建控件,并绑定控件事件
type TForm13 = class(TForm) Button1: TButton; ScrollBox1: TScrollBox; procedure Button1Click(Sender: TObject); private { Private declarations } procedure myClick(sender: TObject); public { Public declarations } end; var Form13: TForm13; implementation {$R *.dfm} procedure TForm13.Button1Click(Sender: TObject); const iHtoH = 20; //行间距 iWtoW = 10; //列间距 iPerLine = 6; //每行个数 iWidth = 80; //按钮宽度 var i: Integer; begin for i := 1 to 30 do begin with TButton.Create(Self) do begin Name := 'Btn' + InttoStr(i); Parent := Self.ScrollBox1; Caption := Name; Top := iHtoH + (30 + iHtoH) * (i div iPerLine - integer((i mod iPerLine) = 0)); Left := iWtoW + (iWidth + iWtoW) * ((i - 1) mod iPerLine); if Name = 'Btn5' then begin OnClick := myClick; end; Show; end; end; end; procedure TForm13.myClick(sender: TObject); begin ShowMessage('hello,world!'); end;