Use variables for object name in Delphi

后端 未结 3 823
死守一世寂寞
死守一世寂寞 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:42

    If you want to change all labels on the form, you can use something like this:

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

    If labels are on Panel or some other container, you can limit this by replacing Form1 by eg "Form1.Panel1". You can also use eg. the tag property of components to easily select which labels you want to change.

提交回复
热议问题