?
Solved

array of labels?

Posted on 2000-01-30
7
Medium Priority
?
260 Views
Last Modified: 2010-04-04
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:)
0
Comment
Question by:Brooky
7 Comments
 
LVL 1

Expert Comment

by:Vendi
ID: 2440944
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
 
LVL 1

Expert Comment

by:XAN
ID: 2441062
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
 
LVL 6

Accepted Solution

by:
DrDelphi earned 260 total points
ID: 2441081
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
LVL 1

Expert Comment

by:XAN
ID: 2441121
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
 
LVL 17

Expert Comment

by:inthe
ID: 2441222
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2441941
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
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 2461703
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question