Board index » delphi » Newbie Q about loops

Newbie Q about loops

I just started playing around with Delphi and I want to use a for loop
to assign the letters A, B, C ... to a series of buttons, ie

Button1.Caption:='A', Button2.Caption:='B' etc.

Can I use the counter variable in the for loop to cycle through the
buttons like

ButtonX.Caption:=letter;

where X is replaced by the counter variable?

Thanks

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

 

Re:Newbie Q about loops


Use the Controls[i] of the form

for I:= 0 to Form1.ControlCount -1 do
begin
    with Form1.Controls[I] do
    begin
        if copy(Name,1,6)='Button' then Caption:=chr(ord('A')+i);
    end;
end;

Look in the help by controls propety of a form

james_lev...@my-deja.com heeft geschreven in bericht
<7mjtfm$6u...@nnrp1.deja.com>...

Quote
>I just started playing around with Delphi and I want to use a for loop
>to assign the letters A, B, C ... to a series of buttons, ie

>Button1.Caption:='A', Button2.Caption:='B' etc.

>Can I use the counter variable in the for loop to cycle through the
>buttons like

>ButtonX.Caption:=letter;

>where X is replaced by the counter variable?

>Thanks

>Sent via Deja.com http://www.deja.com/
>Share what you know. Learn what you don't.

Re:Newbie Q about loops


james_lev...@my-deja.com schrieb in Nachricht
<7mjtfm$6u...@nnrp1.deja.com>...

Quote
>I just started playing around with Delphi and I want to use a for loop
>to assign the letters A, B, C ... to a series of buttons, ie

>Button1.Caption:='A', Button2.Caption:='B' etc.

>Can I use the counter variable in the for loop to cycle through the
>buttons like

>ButtonX.Caption:=letter;

>where X is replaced by the counter variable?

>Thanks

For this purpose you can use the FindComponent function.

One example:

for cnt:=1 to 26 do begin
    TButton(FindComponent('Button'+IntToStr(cnt))).Caption:=Chr(64+cnt);
end;

You need to typecast the result of FindComponent because it returns
TComponent...

Hth,
Matthias.

Other Threads