Use variables for object name in Delphi

后端 未结 3 832
死守一世寂寞
死守一世寂寞 2021-01-16 13:25

I\'m trying to change the caption of many labels using regular way:

form1.label1.caption := \'1\';
form1.label2.caption := \'2\';
form1.label3.c         


        
3条回答
  •  余生分开走
    2021-01-16 13:57

    Create your controls dynamically. If you need to retain references to them hold those references in an array. For example, this is the general pattern.

    var
      FLabels: array of TLabel;
    ....
    SetLength(FLabels, Count);
    for i := 0 to Count-1 do
    begin
      FLabels[i] := TLabel.Create(Self);
      FLabels[i].Parent := Self;
      FLabels[i].Caption := IntToStr(i+1);
      FLabels[i].Left := 8;
      FLabels[i].Top := 8 + i*20;
    end;
    

提交回复
热议问题