Board index » delphi » Creating Arrays of Delphi Visual Objects at Runtime

Creating Arrays of Delphi Visual Objects at Runtime

Hiya.

We're having a problem with Delphi ...

At the moment, we've implemented a form which displays text on a bitmap
background.  I'm displaying each line of text on a label.  I know
there are better ways of displaying text, but we've got reasons for
doing it this way.

We've done this by putting 10 labels on the form - one label for each
line, and naming them TextLab1..TextLab10, and hardcoding the code to
suit.  Yuk.

My question - can I create an array of labels at runtime, set their
properties (position, font etc) and then change their caption,
to display the text?

I can declare them no problemo with:

  TextLines: array [1..10] of TLabel;

And in the form's OnCreate proc:

  for i := 1 to 10 do
    TextLines[i] := TLabel.Create;

However, I get a compiler error, saying it expects a '(' after the
Create.  Rats ...

Anyone out there who has done this successfully?

Thanks,

Roddi

 

Re:Creating Arrays of Delphi Visual Objects at Runtime


Quote
Roddi Walker (ro...@cs.monash.edu.au) wrote:

: Hiya.

[munch]

: I can declare them no problemo with:
:   TextLines: array [1..10] of TLabel;
: And in the form's OnCreate proc:
:   for i := 1 to 10 do
:     TextLines[i] := TLabel.Create;
: However, I get a compiler error, saying it expects a '(' after the
: Create.  Rats ...

Try TextLines[i]:=TLabel.Create(self), since a component .Create always
excpects the owner as a parameter to its constructor.

--
Ebbe Jonsson, Systems Mangler                           e...@cardinal.fi
Tel: +358 53 553 53 97                                  e...@dbm.fi
Fax: +358 0 33 88 22                                    " Why me? "

Re:Creating Arrays of Delphi Visual Objects at Runtime


Quote
e...@myhost.subdomain.domain (Ebbe Jonsson) wrote:

> Roddi Walker (ro...@cs.monash.edu.au) wrote:
> : Hiya.

> [munch]

> : I can declare them no problemo with:
> :   TextLines: array [1..10] of TLabel;
> : And in the form's OnCreate proc:
> :   for i := 1 to 10 do
> :     TextLines[i] := TLabel.Create;
> : However, I get a compiler error, saying it expects a '(' after the
> : Create.  Rats ...

> Try TextLines[i]:=TLabel.Create(self), since a component .Create always
> excpects the owner as a parameter to its constructor.

You will have to set the parent as well:

TForm1.SomeProc
var
  I: integer;
begin
   :
  for I := 1 to 10 do begin
    TextLines[I] := TLabel.Create(Self);
    TextLines[I].Parent := Self;
  end;
  :

=== Mark Patterson

Other Threads