在Delphi中可以运用Tcontrol数组吗?
比如:
  有数一百个相同控件,想使得label1.caption:=.....
                      label2.caption:=....
                      .
                      .
                      .
  一个个写太烦,如果能建个数组A[1..100],再用循环语句那就好了。但事实上我已试过 delphi中不能用label[1],label[2].........好像vb中可以建立控件数组的,请教高手们在delphi中该怎么做呢?

解决方案 »

  1.   

    (findcomponent('label'+inttostr(i)) as tlabel).caption:=.....
    i 循环  即可
      

  2.   

    var i:integer;
    begin
      for i:=0 to self.ComponentCount-1 do
      begin
        if Componect[i].calssType=TLabel then TLabel(Componect[i]).Caption:='aaa';
      end;
    end;
      

  3.   

    这里给你一个tlabel的动态生成方法并追加动态事件的简单例子,其它类可以参考啦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.
      

  4.   

    ITlover你好,谢谢你回复,你的方法我试过了,窗口不会有任何显示,不晓得是那出了问题,你自己可以试试看。
      

  5.   

    楼主,上面兄弟说的可行的,不过有些地方需要注意一下:  1、在新建一个工程后先拖一个Button控件到窗体中,然后将ITLover兄的代码复制过去替换掉你工程中的unit单元文件(就是在表单上按F12时看到的)
      2、然后不要去运行程序,否则运行后你去按button1是没有反应的,请务必在表中双击一下button1后保存再运行即可,至于什么原因回去好好想想吧!想通后保证你大有收获如果成功了就告诉大家一下,不要自己成功了就不以为是一回事了
      

  6.   

    不好意思字打错了,上面2中应该是在表单中双击一下button1控件,(然后你会看见unit文件),好这下保存一下再运行就ok了