Board index » delphi » Putting values into many labels

Putting values into many labels

I would be grateful for any pointers on solving the following Delphi
problem that a friend of mine has:

Normally you set the value of a label on a tabbed page by writing e.g.
  label1.caption := 3;
but what if you have only a string (with name say STRNG) at run time
with value e.g. 'label9'.  How can you then set label9 to 3 ?  This must
be possible at run time.

You could of course write
  label9 := 3;
but if you do not want to write a separate sentence for label1, label2,
label3, label4,...label999,...etc, ... but want instead to use a SINGLE
LABEL ASSIGNMENT STATEMENT (IN A LOOP) TO SET ALL THE LABELS from the
label names generated as strings at run time, how should you do that?

So far I see that the labels are components and so have
component-numbers.  Given the string, we can find the component number
either by brute search or a library routine that gives you the number if
you have the name (which we do, as a string).  Any suggestions as to
which library routine that might be?

And, if we get the component-number for the label name in our string, we
should really be able to call a library routine that puts a value into
the component with the given number.  Any suggestions about that,
please?

My friend and I have not seen any routines like that in the HELP files
but they must exist internally in Delphi for it to work, I think.
Presumably there is some classic standard method for doing this, that we
do not see in the help files, or in the books by Calvert and the Waites
Group, although perhaps we have not looked closely enough yet.

Grateful for any help on this question.

Regards,   David Walker     mar...@debet.nhh.no

 

Re:Putting values into many labels


Quote
mar...@debet.nhh.no wrote:
>I would be grateful for any pointers on solving the following Delphi
>problem that a friend of mine has:
>Normally you set the value of a label on a tabbed page by writing e.g.
>  label1.caption := 3;
>but what if you have only a string (with name say STRNG) at run time
>with value e.g. 'label9'.  How can you then set label9 to 3 ?  This must
>be possible at run time.
>You could of course write
>  label9 := 3;
>but if you do not want to write a separate sentence for label1, label2,
>label3, label4,...label999,...etc, ... but want instead to use a SINGLE
>LABEL ASSIGNMENT STATEMENT (IN A LOOP) TO SET ALL THE LABELS from the
>label names generated as strings at run time, how should you do that?
>So far I see that the labels are components and so have
>component-numbers.  Given the string, we can find the component number
>either by brute search or a library routine that gives you the number if
>you have the name (which we do, as a string).  Any suggestions as to
>which library routine that might be?
>And, if we get the component-number for the label name in our string, we
>should really be able to call a library routine that puts a value into
>the component with the given number.  Any suggestions about that,
>please?
>My friend and I have not seen any routines like that in the HELP files
>but they must exist internally in Delphi for it to work, I think.
>Presumably there is some classic standard method for doing this, that we
>do not see in the help files, or in the books by Calvert and the Waites
>Group, although perhaps we have not looked closely enough yet.
>Grateful for any help on this question.
>Regards,   David Walker     mar...@debet.nhh.no

There area several ways to do this:

1 -- If you name your labels (actually keep your labels) as Label1,
Label2, etc., this is simple:

procedure Tform1.AssignLabels(s: String);
var MyLabel: TComponent;
      i: Integer;
begin
  For i := 1 To 9 Do
  Begin
     MyComponent := FindComponent('Label' + IntToStr(i));
    TLabel(MyComponent).Caption := s;
  End;
end;

You can also (on the form's OnCreate event) initialize an array of
TLabels that you wish to assign.  I have declared an array in the
PRIVATE area of the form named MyLabels: Array[1..9] of TLabel:

procedure TForm1.OnCreate(Sender: TObject);
begin
  MyLabels[1] := lblFirstName;
  MyLabels[2] := lblLastName;
  MyLabels[3] := lblAddress;
  :
  :
end;

procedure TForm1.ChangeCaptions(s: String);
var i: Integer;
begin
  For i := 1 To 9 Do
    MyLabels[i] := s;
end;

Mike Sherrane
sherr...@bluemtn.com                blue mountain software

Our motto: "Work hard,have fun,make money."
(Delphi,Progress,C++, & Rad.  Atlanta, Georgia. 770- 875-8324)

sherr...@bluemtn.com                blue mountain software

Our motto: "Work hard,have fun,make money."
(Delphi,Progress,C++, & Rad.  Atlanta, Georgia. 770- 875-8324)

Other Threads