Board index » delphi » Newbie: Looping through Labels ?

Newbie: Looping through Labels ?

Hi All,
        I have numerous lables, which I would like to loop through and
change the colour ( and / or caption )  of certain labels.
Is it possible to have an ARRAY OF CONTROLS (Labels) as in VB.

I would like to do something like..

for num = 4 to 10 do
        (label + num).caption := num;

I want to be able to change to the label4 by adding the 4 to the
label.

Any ideas, Thanks in Advance.  Ken

 

Re:Newbie: Looping through Labels ?


try something like this..

procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
 for i:=0 to controlcount -1 do
  if (controls[i] is tlabel) then tlabel(controls[i]).caption:='delphi owns
vb';
end;

if you only wanted to change certain labels youd just change their tag or
somthin to set them apart from other labels and then in the loop youd compare
tags.. its pretty straight forward.

Quote
>Subject: Newbie: Looping through Labels ?
>From: ktay...@cheapnet.co.uk (Ken Taylor)
>Date: Sun, Jan 31, 1999 18:27 EST
>Message-id: <36b5e41a.22429...@news.cheapnet.co.uk>

>Hi All,
>    I have numerous lables, which I would like to loop through and
>change the colour ( and / or caption )  of certain labels.
>Is it possible to have an ARRAY OF CONTROLS (Labels) as in VB.

>I would like to do something like..

>for num = 4 to 10 do
>    (label + num).caption := num;

>I want to be able to change to the label4 by adding the 4 to the
>label.

>Any ideas, Thanks in Advance.  Ken

Re:Newbie: Looping through Labels ?


Hi...

See TForm's properties :
   * ComponentCount    * Components
You can check all components in that form.
It's TLabel or not.

Or use Tlabel.Tag to identify the object.

I hope it's useful.

Quote
Ken Taylor wrote:

> Hi All,
>         I have numerous lables, which I would like to loop through and
> change the colour ( and / or caption )  of certain labels.
> Is it possible to have an ARRAY OF CONTROLS (Labels) as in VB.

> I would like to do something like..

> for num = 4 to 10 do
>         (label + num).caption := num;

> I want to be able to change to the label4 by adding the 4 to the
> label.

> Any ideas, Thanks in Advance.  Ken

Re:Newbie: Looping through Labels ?


On Mon, 01 Feb 1999 12:55:30 +0700, Ferry Suryanto

Quote
<afei...@softhome.net> wrote:
>Hi...

>See TForm's properties :
>   * ComponentCount    * Components
>You can check all components in that form.
>It's TLabel or not.

>Or use Tlabel.Tag to identify the object.

>I hope it's useful.

Cheers, Components worked for me.

I ended up with this gibberish :-)

    if components[num*5-3] is tlabel then

       for co := 1 to 4 do
           tlabel(components[((num*5)-4)+co]).color
:=tedit(components[co+98]).color;

    if components[num*5+1] is Tlabel then
       Tlabel(components[num*5+1]).caption := inttostr(correct);

Good eh ?

Thanks again.

Re:Newbie: Looping through Labels ?


Assuming you named your labels Label1, Label2, etc.  you can use:

  for Num := 1 to 10 do
     TLabel(FindComponent('Label' + IntToStr(Num))).Caption :=
IntToStr(Num);

Bye,
R. Mittendorff
the Netherlands

Ken Taylor heeft geschreven in bericht
<36b5e41a.22429...@news.cheapnet.co.uk>...

Quote
>Hi All,
> I have numerous lables, which I would like to loop through and
>change the colour ( and / or caption )  of certain labels.
>Is it possible to have an ARRAY OF CONTROLS (Labels) as in VB.

>I would like to do something like..

>for num = 4 to 10 do
> (label + num).caption := num;

>I want to be able to change to the label4 by adding the 4 to the
>label.

>Any ideas, Thanks in Advance.  Ken

Other Threads