在Delphi中可以运用Tcontrol数组吗?
比如:
有数一百个相同控件,想使得label1.caption:=.....
label2.caption:=....
.
.
.
一个个写太烦,如果能建个数组A[1..100],再用循环语句那就好了。但事实上我已试过 delphi中不能用label[1],label[2].........好像vb中可以建立控件数组的,请教高手们在delphi中该怎么做呢?
另外执行以下的程序后,
unit Unit1;interfaceuses
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.
窗口并没显示控件组,知道是出了什么问题吗?
比如:
有数一百个相同控件,想使得label1.caption:=.....
label2.caption:=....
.
.
.
一个个写太烦,如果能建个数组A[1..100],再用循环语句那就好了。但事实上我已试过 delphi中不能用label[1],label[2].........好像vb中可以建立控件数组的,请教高手们在delphi中该怎么做呢?
另外执行以下的程序后,
unit Unit1;interfaceuses
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.
窗口并没显示控件组,知道是出了什么问题吗?
这句不要放在循环中。procedure TForm1.SpeedButton1Click(Sender: TObject);
var
MyLabelArray: array of TLabel;
i: Integer;
begin
SetLength(MyLabelArray, 10);
for i := 0 to High(MyLabelArray) do
begin
MyLabelArray[i] := TLabel.Create(Self);
MyLabelArray[i].Caption := 'One';
MyLabelArray[i].Parent := Self;
MyLabelArray[i].Left := i * 10;
MyLabelArray[i].Top := i * 10;
MyLabelArray[i].OnClick := MyLabelClick;
end;
end;
if Form1.Components[i] is TLabel then
TLabel(Form1.Components[i]).Caption=IntToStr(i)
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.