Board index » delphi » Using a loop to control components

Using a loop to control components

How can I, if I can, control components in a loop? Let's say I wanna give 25
buttons the caption of 1-25, do I have to write this all out, or can I do it
in a for-next loop too? that would save about 22 lines! any help would be
appreciated!
 

Re:Using a loop to control components


John,
Maybe you should try something like this:

procedure SetButtonCaptions;
var    BtnNo: Integer;
begin
  for BtnNo := 1 to 25 do
    TButton(FindComponent('Button' + IntToStr(BtnNo))).Caption :=
IntToStr(BtnNo);
end;

Regards
Chris

Quote
John DeRama <oudend...@home.nl> wrote in message

news:hZn27.206095$u5.4069863@zwoll1.home.nl...
Quote
> How can I, if I can, control components in a loop? Let's say I wanna give
25
> buttons the caption of 1-25, do I have to write this all out, or can I do
it
> in a for-next loop too? that would save about 22 lines! any help would be
> appreciated!

Re:Using a loop to control components


Hi John,

Quote
>How can I, if I can, control components in a loop? Let's say I wanna give 25
>buttons the caption of 1-25, do I have to write this all out, or can I do it
>in a for-next loop too? that would save about 22 lines! any help would be
>appreciated!

[just scribbled - untested]
for i:= 0 to ComponentCount - 1 do
  if Components[i] is TButton then
    TButton(Components[i]).Caption:= IntToStr(i);

Cheers,
   Udo
--
Homepage: http://www.nesshoever.de            No mails please. Reply here.

Delphi env.:                         Global Polio Awareness Campaign 2001+
W2K.sp2, D6Pro & D4Pro.sp3               More info -> http://www.2-mad.com

Re:Using a loop to control components


Quote
"John DeRama" <oudend...@home.nl> wrote in message

news:hZn27.206095$u5.4069863@zwoll1.home.nl...

Quote
> How can I, if I can, control components in a loop? Let's say I wanna give
25
> buttons the caption of 1-25, do I have to write this all out, or can I do
it
> in a for-next loop too? that would save about 22 lines! any help would be
> appreciated!

Try the free RBLComponentArray component on the downloads page at
www.bancoems.com/rbaddon.htm

Other Threads