在Delphi中可以运用Tcontrol数组吗?
比如:
有数一百个相同控件,想使得label1.caption:=.....
label2.caption:=....
.
.
.
一个个写太烦,如果能建个数组A[1..100],再用循环语句那就好了。但事实上我已试过 delphi中不能用label[1],label[2].........好像vb中可以建立控件数组的,请教高手们在delphi中该怎么做呢?
比如:
有数一百个相同控件,想使得label1.caption:=.....
label2.caption:=....
.
.
.
一个个写太烦,如果能建个数组A[1..100],再用循环语句那就好了。但事实上我已试过 delphi中不能用label[1],label[2].........好像vb中可以建立控件数组的,请教高手们在delphi中该怎么做呢?
i 循环 即可
begin
for i:=0 to self.ComponentCount-1 do
begin
if Componect[i].calssType=TLabel then TLabel(Componect[i]).Caption:='aaa';
end;
end;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure myLabelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
mylabel: array of tlabel;
i:integer;
mytop,myleft:integer;
implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject); //这一过程完成动态控件生成
begin
i:=0;
mytop:=10;
myleft:=10;
while i<=9 do
begin
setlength(mylabel,i+1);
mylabel[i]:=tlabel.Create(self);
mylabel[i].Parent:=form1;
mylabel[i].Caption:='mylabel'+inttostr(i);
mylabel[i].Left:=myleft;
mylabel[i].Top:=mytop;
mylabel[i].OnClick:=mylabelclick;
i:=i+1;
mytop:=mytop+20;
end;
end;procedure TForm1.myLabelClick(Sender: TObject);
begin
showmessage((sender as tlabel).caption);
end;end.
2、然后不要去运行程序,否则运行后你去按button1是没有反应的,请务必在表中双击一下button1后保存再运行即可,至于什么原因回去好好想想吧!想通后保证你大有收获如果成功了就告诉大家一下,不要自己成功了就不以为是一回事了