我添了几个LABEL比如:Label1、Label2、Label3…………Label9;现在我想把它们FONT的颜色全更改为绿色;我打算用一个循环来做,但着手的时候就发现做不下去了,请各位帮帮忙!我是这样子做的:        for i:=1 to 9 do
          begin
             str:='Label'+inttostr(i);
          end;       到这里可以用STR变量保存对象名字符串,但却又不能调用对象的属性拉:
      Labeln.font.color:= clGreen;     就是说我总不能用str.font.color:= clGreen;吧,我该怎么做?

解决方案 »

  1.   

    创建控件数组:type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
        Tab:array[0..3] of TLabel;
        procedure myClick(Sender:TObject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}//自定义的Label的OnClick事件处理过程
    procedure TForm1.myClick(Sender: TObject);
    var
      s:String;
    begin
      if Sender is TLabel then
        s:=TLabel(Sender).Caption
      else
        s:='Hello!';//这改了!  ShowMessage(s);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      i:Integer;
    begin
      for i := Low(Tab) to High(Tab) do
      begin
        Tab[i]:=Tlabel.Create(Self);
        tab[i].OnClick:=MyClick;//在这里付值
        Tab[i].Parent:=Self;
        Tab[i].Top := 20 + i * 50;
        Tab[i].Left :=70;
        Tab[i].Caption := 'Label' + IntToStr(i);
        Tab[i].Font.Color:= clGreen;
        Tab[i].Visible :=true;
      end;
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      Tab[2].OnClick(Sender);//直接调用,传入参数应该与过程申明相同!
    end;end.
      

  2.   

    for i:=1 to 9 do
              begin
                 str:='Label'+inttostr(i);
                 TLabel(FindComponent(str)).Font.Color := clGreen;
              end;
      

  3.   

    TO :wx1452(),我已经包含有了STDCTRLS,但TLabel()还是不能用。错误是这样的:第一个LABEL是把颜色该过来了,但循环到了第二个的时候,就出现了异常,提示如下————Access Violation at address 0045A1A9 in module 'PROJECT1.EXE'.Read of address 'FFFFFFFF'.为什么会这样?