在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.
   窗口并没显示控件组,知道是出了什么问题吗?

解决方案 »

  1.   

    setlength(mylabel,i+1);
    这句不要放在循环中。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;
      

  2.   

    加上 mylabel[i].Visible := True;
      

  3.   

    for i:=0 to form1.ComponentCount-1 do
       if Form1.Components[i] is TLabel then
         TLabel(Form1.Components[i]).Caption=IntToStr(i)
      

  4.   

    使用 Form1 的 Components  数组,虽然没有VB的控件数组方便不过一定能完成那个功能你的Label一要顺序放下去,不然他对应的i会变的
      

  5.   

    Tlabel(frmoutline.FindComponent('label1'+inttostr(i))).Color:=clYellow;
      

  6.   

    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.