我想写一个应用程序,需要为该程序动态的添加控件。
比如:我需要在Form1上再添加若干个按钮(Button2,Button3......),并且为这些动态生成的按钮分别提供“Click”事件(showmessage('this is Button2')......)。急需代码,谢谢。
比如:我需要在Form1上再添加若干个按钮(Button2,Button3......),并且为这些动态生成的按钮分别提供“Click”事件(showmessage('this is Button2')......)。急需代码,谢谢。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtDlgs, StdCtrls;type
TForm1 = class(TForm)
OpenPictureDialog1: TOpenPictureDialog;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure ButtonClick(Sender: TObject);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.ButtonClick(Sender: TObject);
begin
showmessage((sender as Tbutton).Caption);
end;
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
for i:=0 to 4 do
with TButton.Create(self) do
begin
left:=i*80;
top:=100;
name:='Button_'+inttostr(i);
caption:=name;
parent:=self;
Onclick:=ButtonClick;
end;
end;end.