有一个函数
function massbutton(a:integer);integer;
var i:integer;
mybutton:array of Tbutton;
begin
setlength(mybutton,a);
for i:=0 to a-1 do
begin
mybutton[i]:=Tbutton.create(from1);
mybutton[i].parent:=form1;
mybutton[i].left:=100+i*2;
mybutton[i].top:=200+i*2;
mybutton[i].caption:='this is No.'+inttostr(i)+'button';
end;
massbutton:=1;
end;请问如何给这些每一个按钮的onclick事件编写代码?
function massbutton(a:integer);integer;
var i:integer;
mybutton:array of Tbutton;
begin
setlength(mybutton,a);
for i:=0 to a-1 do
begin
mybutton[i]:=Tbutton.create(from1);
mybutton[i].parent:=form1;
mybutton[i].left:=100+i*2;
mybutton[i].top:=200+i*2;
mybutton[i].caption:='this is No.'+inttostr(i)+'button';
end;
massbutton:=1;
end;请问如何给这些每一个按钮的onclick事件编写代码?
--------------------------------------------------------------------------------
我很菜,就以showmessage(inttostr(i));为例子教我一下吧。
然后,在按钮创建后
//...
mybutton[i].top:=200+i*2;
mybutton[i].caption:='this is No.'+inttostr(i)+'button';
mybutton[i].Name := Btn+IntToStr(i);
mybutton[i].Onclick := btnclick;
//...
然后,在公共事件里面根据sender的名字判断进行处理各自的事件,如:
procedure TForm1.btnclick(sender:TObject);
begin
case (sender as TButton).Name of
'btn1':{button[1]的事件}
'btn2':{button[2]的事件};
//...
end;
------------------------------------------------------------------------------
这是啥玩意儿?
case (sender as TButton).Name of
'btn1':{button[1]的事件}
'btn2':{button[2]的事件};
//...
不是很无稽吗?
[Error] Unit1.pas(38): Undeclared identifier: 'btnclick'
case (sender as TButton).Name of
'btn1':{button[1]的事件}
'btn2':{button[2]的事件};
//...
不是很无稽吗?
--------------------------
怎么会不知道有多少个?
for i:=0 to a-1 do 你的这句代码不是代表了多少个button吗?
-----------------------------
函数function中不能调用procedure吗?
[Error] Unit1.pas(38): Undeclared identifier: 'btnclick'你要先定义btnclick事件
在form的publish 里写上
procedure btnclick(sender:TObject);
然后在实现部分写上procedure TForm1.btnclick(sender:TObject);
begin
case (sender as TButton).Name of
'btn1':{button[1]的事件}
'btn2':{button[2]的事件};
//...
end;
麻烦一点的,你可以通过消息的形式去执行!每一个Button的编号加上一个固定的书就可以对应一个Windows消息.
for i:=0 to a-1 do
begin
mybutton:=Tbutton.create(from1);
....
mybutton.name:='button_'+inttostr(i);
end;
寻找该组件 tbutton(findconponent('')).