我在form 中建立了一个动态按钮数组,但我不知道这么使用他们,请大虾指教
for i:=1 to strtoint 5 do
begin
button[i] := Tbutton.create(self);
with button[i] do
begin
Parent := Self;
top:= atop;
left:=30;
width:=100;
height := 30;
atop:=atop+30;
caption:='cj'+inttostr(i);
end;
end;我建了这5个按钮后不知道怎么使用他们,如何点击他们后可以运行事件;
for i:=1 to strtoint 5 do
begin
button[i] := Tbutton.create(self);
with button[i] do
begin
Parent := Self;
top:= atop;
left:=30;
width:=100;
height := 30;
atop:=atop+30;
caption:='cj'+inttostr(i);
end;
end;我建了这5个按钮后不知道怎么使用他们,如何点击他们后可以运行事件;
with button[i] do
begin
Parent := Self;
top:= atop;
left:=30;
width:=100;
height := 30;
atop:=atop+30;
caption:='cj'+inttostr(i);
OnClick := ButtonClick;{procedure TForm1.ButtonClick(Sender: TObject);}
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}var
buttons: array[1..5] of TButton;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Button "' + TButton(Sender).Caption + '" Clicked!');
end;procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
atop: integer;
begin
atop := 20;
for i:=1 to 5 do
begin
buttons[i] := Tbutton.create(self);
with buttons[i] do
begin
Parent := Self;
top:= atop;
left:=30;
width:=100;
height := 30;
atop:=atop+30;
OnClick := Button1Click;
caption:='cj'+inttostr(i);
end;
end;
end;end.
for i:=1 to 5 do
begin
button[i] := Tbutton.create(Owner);
with button[i] do
begin
Parent := Self;
top:= atop;
left:=30;
width:=100;
height := 30;
atop:=atop+30;
caption:='cj'+inttostr(i);
OnClick := ButtonClick;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(TButton(Sender).Caption+' test');
end;procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
atop: integer;
begin
atop := 20;
for i:=1 to 5 do
begin
buttons[i] := Tbutton.create(self);
with buttons[i] do
begin
Parent := Self;
top:= atop;
left:=30;
width:=100;
height := 30;
atop:=atop+30;
caption:='cj'+inttostr(i);
OnClick := Button1Click;
end;
end;
end;
每个button可以根据不同的tag进行click处理。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}var
buttons: array[1..5] of TButton;procedure TForm1.Button1Click(Sender: TObject);
begin
//如果每個Button都執行一樣的操作
ShowMessage('Button "' + TButton(Sender).Caption + '" Clicked!');
//如果每個Button每個都有各自的功能
case TButton(Sender).Tag of
1:showmessage('1');
2:showmessage('2');
3:showmessage('3');
end;
end;procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
atop: integer;
begin
atop := 20;
for i:=1 to 5 do
begin
buttons[i] := Tbutton.create(self);
with buttons[i] do
begin
Parent := Self;
top:= atop;
left:=30;
width:=100;
height := 30;
atop:=atop+30;
OnClick := Button1Click;
caption:='cj'+inttostr(i);
Tag:=i;
end;
end;
end;end.