1

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

form1.label1.caption := '1';
form1.label2.caption := '2';
form1.label3.caption := '3';
form1.label4.caption := '4';
form1.label5.caption := '5';
form1.label6.caption := '6';
form1.label7.caption := '7';
form1.label8.caption := '8';
...

How can I use For and assign i to label name like Label[i]? Something like this:

for i := 1 to 50 do
   begin
     form1.label[i].caption := Inttostr(i);
   end;

And what is the best way to change too many object parameters (In this case caption)?

6
  • 3
    Take a look here stackoverflow.com/questions/16710189/…. Or just use Findcomponent to search the site. Commented Sep 15, 2013 at 13:38
  • @bummi why did you suggest to use Findcomponent? Commented Sep 15, 2013 at 16:05
  • 3
    Thanks @bummi for the question, but I did not suggest to use it, but to use it for search, since this has been asked often before in different colors and questions containing FindComponent show different solutions besides to FindComponent. Since I know it's sometimes difficult without the keyword to find existing solutions I decided not to search for a exact duplicate for closing. Commented Sep 15, 2013 at 16:10
  • @SertacAkyuz sorry for the monologue ;-) Commented Sep 15, 2013 at 16:12
  • 2
    Hey, @FreeConsulting why bummi talks to himself? Commented Sep 15, 2013 at 18:22

3 Answers 3

14

If you are sure you have 50 labels label1, label2 .. label50
The solution could be like this:

var lbl: TLabel;
for i:=1 to 50 do
begin
    lbl := FindComponent('Label'+IntToStr(i)) as TLabel;
    lbl.Caption := IntToStr(i);
end;
Sign up to request clarification or add additional context in comments.

5 Comments

@AndreasRejbrand Why you don't like this? What's wrong with this?
A numebr of things. For one thing, FindComponent takes some time to compute; you need to compare a lot of strings. This you don't have to do if you use arrays. Second, using arrays you get 100 % control over the life of the controls. You can create them dynamically, change them dynamically, and remove them dynamically.
FindComponent binds to the control at runtime. If you can bind at compile time then the compiler can check correctness. If you change the name of the components, you only discover FindComponent failures at runtime. Always prefer compile time solutions to runtime solutions. If there are no compile time solutions, use runtime solution. Otherwise, prefer compile time.
FindComponent is unlikely to become a bottleneck.
@Free Agreed. Not a bottleneck. More about aesthetics, style maintainability etc.
9

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;

1 Comment

Thanks. Nice code. Can I use this in threads? I mean is this Thread-safe?
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.