大家好!我想用一个循环的方式实现一组button,且是动态创建的!我能实现了一个button,那是因为我定义了一个tbutton类型,但是如果我是想要是实现100个button,我要开始定义100 button吗?这是很吓人的!希望有人能不吝赐教!这个想法是我从java中模仿过来的,但是我的delphi才学了一个多月,实在不知道怎样去做!以下是我做的实现一个button的代码!
procedure Tanother_form.Button1Click(Sender: TObject);
var
button2:tbutton;
begin
button2.Create(nil);
button2.Width:=85;
button2.Left:=100;
button2.Show;
end;
再次感谢!
procedure Tanother_form.Button1Click(Sender: TObject);
var
button2:tbutton;
begin
button2.Create(nil);
button2.Width:=85;
button2.Left:=100;
button2.Show;
end;
再次感谢!
var i,j :integer;
begin
for i:=0 to 9 do
begin
for j:=0 to 9 do
begin
with Tbutton.Create(self) do
begin
parent := self;
left :=width*j;
top :=height*i;
end; end;
end;
end;
type aa=array[1..10] of Tbutton;//你要定义几个Button,就把数组定义成多大;
var bb:aa;
创建:
for i:=1 to 10 do //for 循环次数由数组最大长度来决定
begin
bb[i]=Tbutton.create(self);
bb[i].parent:=Tform1 //父的值为你所创建的窗体。self也行。
end;
但是那样创建Button的name是什么呢?
var
mybutton:array of tbutton;
i:integer;
begin
setlength(mybutton,4);
for i:=0 to 3 do
begin
mybutton[i]:=tbutton.Create(self);
mybutton[i].Parent:=form1;
mybutton[i].Caption:='dadadf';
mybutton[i].Left:=100+i*100;
mybutton[i].Height:=25;
mybutton[i].Top:=50;
mybutton[i].Width:=50;
mybutton[i].Visible:=true;
end;
end;
看看这个,应该可以实现的。
var i,j :integer;
for i:=0 to 9 do
begin
for j:=0 to 9 do
begin
with Tbutton.Create(self) do
begin
parent :=self;
left :=width*j;
top :=height*i;
caption :='button'+inttostr((i*10+j));
name :='button'+inttostr((i*10+j));
end;
end;
end;
for i:=0 to 9 do
begin
for j:=0 to 9 do
begin
with Tbutton.Create(self) do
begin
parent :=self;
left :=width*j;
top :=height*i;
caption :='button'+inttostr((i*10+j));
name :='button'+inttostr((i*10+j));
end;
end;
end;
另外,我认为加名字方法可以写一个过程,这样显得有对象的思想!因为创建的每一个button是一个新的对象,这样自身还有独立性!(不知道这样说对不对?反正我感觉将方法外置是很好的编程习惯!)