我想问下 变量A为随即的整数当我按下button1后  那label(A).caption='A'
这该怎么写?  

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
     var
      I: Integer;
    begin
      I :=Random(10)+1;  TLabel(Components[I]).Caption := inttostr(I);
    end;
      

  2.   

    1楼的写法有问题,如果随机出来的不是Component不是TLabel的话会报错,改为如下:procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
    begin
      for i := 0 to self.ComponentCount - 1 do
      begin
        if Components[i] is TLabel then
           TLabel(Components[i]).Caption := 'Test' + IntToStr(i);
      end;
    end;