array of labels?

ok people, i am developing an application which requires me to be referring to any one of 42 labels that i have defined. It would be extremely labourous for me to do an individual procedure to go with each label, what i need to do is write a procedure that can altar any one of the labels depending on a passed parameter. E.g instead of having to refer to label1.caption (or whatever, i could refer to label[n].caption, ok? if anyone knows how i can achive this effect then please let me know:)
BrookyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VendiCommented:
http://www.edgequest.com/Development/DelphiArticles.htm

This site has an article about control arrays in Delphi.  I think it is what you are looking for.  If it is, hand over the points, ha!  :)
0
XANCommented:
you can use array (or TList?). to initialize it you can do next:

for i := 0 to Form1.ComponentCount - 1 do
    if Form1.Components[i] is TLabel then
  {add to your store - array, Tlist or other}

Then you can refer to it by index.
Note. if use TStringList you can store for example label.caption in Strings and label in Objects
0
DrDelphiCommented:
This is frtom my website... I use Tbuttons in this example, but the functionality is the same for TLabels:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure killme(Sender:tobject);
  private
    { Private declarations }
  public
   arraybuttons:array[0..15] of Tbutton;
    pointer:array[0..15] of integer;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var    d:integer;
begin

     for d:=0 to 15  do
           begin
             arraybuttons[d]:=Tbutton.create(self);
             with arraybuttons[d] do
             begin
               parent:=self;
               caption:=inttostr(d);
               top:=d*21;
               onclick:=killme;
             end;
           end;
end;


Good luck!!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

XANCommented:
you can use array (or TList?). to initialize it you can do next:

for i := 0 to Form1.ComponentCount - 1 do
    if Form1.Components[i] is TLabel then
  {add to your store - array, Tlist or other}

Then you can refer to it by index.
Note. if use TStringList you can store for example label.caption in Strings and label in Objects
0
intheCommented:
you could do something like :

procedure TForm1.Button1Click(Sender: TObject);
var
  i : integer;
n:string;
begin
n := edit1.text;
  for i := 0 to ComponentCount-1 do
    begin
      if( Components[ i ] is TLabel )
and pos(n,components[i].name))
then
      begin
        (Components[ i ] as TLabel).Caption := 'Hello';
      end;
  end;
0
kretzschmarCommented:
hmm,

you can also keep the automatical given names of the labels
(Label1, Label2, Label3, Label4 ... and so on)

and use this code

Procedure TForm1.ChangeLableText(Text : String;Id : Integer);
var TheLabel : TComponent;
begin
  Try
    TheLabel := FindComponent('Label'+IntToStr(Id)); //Label4 for ex
    If (Assigned(TheLabel)) and
       (TheLabel is TLabel) then
      TLabel(TheLabel).Caption := Text;
  Except
    //an error
  end;
end;

//sample for use
procedure TForm1.Button1Click(Sender: TObject);
begin
  ChangeLableText(Edit1.Text,StrToInt(Edit2.Text));
end;

meikl ;-)
0
gandalf_the_whiteCommented:
you could define them statically, as you apparently
know how many labels you have

Labels:array [1..NrOfLabels] of TLabel;

then you have easy access with:
Labels[i].caption

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.