Array of Label zur Entwicklungszeit

  • 1
  • 2
  • Page 1 of 2
Ub.Heinrich
 
Avatar
 
Subject:

Array of Label zur Entwicklungszeit

 · 
Posted: 11.02.2006 - 13:06  ·  #1
Hi @All,

wie kann ich Delphi (7) dazu bringen mir ein Array of TLabel in der Entwicklungszeit zu erstellen um dieses hier zu machen :

Label1[i].Caption := ValueStr[i];


Liebe Grüße
Tassilo
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: Array of Label zur Entwicklungszeit

 · 
Posted: 11.02.2006 - 16:03  ·  #2
Hallo Tassilo,

das ist ein generelles Problem mit Objekten. Aber man kann folgendes tun,
etwas umständlich, aber es funzt:

var
LabelArray : array[0..xx] of tLabel;

Dann wird dieses Array mit den existirenden Labels bevölkert:

LabelArray[0]:= Label0;
LabelArray[1]:= Label1;
...
Das Array enthält jetzt Referenzen (Object-Pointer) auf die einzelnen Labels.

und dann kann so weitergemacht werden:

LabelArray[0].Caption:= 'abc';

rolf
Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1373
Registered: 03 / 2005
Subject:

Re: Array of Label zur Entwicklungszeit

 · 
Posted: 11.02.2006 - 16:40  ·  #3
Hi Tassilo

It is also permitted to create labels (or any other widows object) dynamically, but positioning is the main issue. Still it is a useful technique if the number is not known in advance. Here is a typical example, but be warned - windows objects consume a surprising amount of resource, and often non-windows objects (such as speed-buttons) or using a string array are to be preferred.

Quote

type
tMainForm = class( TForm)
...
private
iMyLabel : array[ 1..cMaxLabelCount ] of tLabel;
public
procedure CreateLabels(ActualCount : integer);
end;

procedure TMainForm.CreateLabels(ActualCount : integer);
var
i, iTop : integer;
begin
iTop := 20;
for i := 1 to ActualCount do
begin
iMyLabel[i] := tLabel.Create( self );
with iMyLabel do
begin
Parent := self;
Left := 20;
Top := iTop;
inc( iTop, 20 );
Label.Caption := ValueStr[ i ];
Show;
end;
end;
end;


You will see, compared to the visual + assignment method, there is a lot involved!

Regards

Merlin :magic:
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: Array of Label zur Entwicklungszeit

 · 
Posted: 11.02.2006 - 16:49  ·  #4
Hi Merlin,

creating labels (in general: visual objects) at runtime is another way to
solve this little problem.
But then you have to calculate their positions at compile time and you
don't have the great support of the form preview which shows the
components. But if the labels/objects are always generated at runtime
yours is the better way.

rolf
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: Array of Label zur Entwicklungszeit

 · 
Posted: 11.02.2006 - 17:00  ·  #5
and there is another way to solve this problem.

Each of these labels must have a unique and known TAG number. So you can
do this:

assuming i is the TAG/number to find
Code
for x:= 0 to componentscount-1 do begin
  if (components[x] is tLabel) and (components[x].Tag = i) then begin
    tLabel(components[x]).caption:= 'abc';
    break;
  end;
end;

I don't know the exact syntax because I rarely use it.

rolf
Ub.Heinrich
 
Avatar
 
Subject:

Re: Array of Label zur Entwicklungszeit

 · 
Posted: 11.02.2006 - 17:04  ·  #6
Hallo Rolf, Merlin,

danke für die Antworten!!

Diese beiden Versionen kannte ich schon!

Aber - die erste Version finde ich aus Faulheit nicht so gut! Viel zu tippen!!

Die zweite geht mir gegen den Strich (auch wenn sie die sauberste ist).
Weil.: Ich habe eine super IDE, wo ich meine Komponenten grafisch sehen kann! Nur weil ich jetzt ein Array von Komponenten haben will, kann ich diese nicht sehen!? Da hat doch der Herr Delphi einen kleinen Hund drinnen!!

Der Herr VB hat sich das Leben etwas vereinfacht indem er eine Proberty namens Index hat.

Liebe Grüße

Tassilo
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: Array of Label zur Entwicklungszeit

 · 
Posted: 11.02.2006 - 17:10  ·  #7
rolfHi Tassilo,

und die dritte Version mit den TAGs ???

Im übrigen hat Delphi die beste Component bzw. Object Konstruktion.
Und manchmal widersprechen Objekte dem quick-and-dirty programmieren.
:lach: :ja: ;D :biggrin:
Ub.Heinrich
 
Avatar
 
Subject:

Re: Array of Label zur Entwicklungszeit

 · 
Posted: 11.02.2006 - 17:24  ·  #8
Hallo Rolf,

Ja, Die mit den Tags scheint mir ganz OK zu sein! Mal sehen wie ich das Ganze jetzt umsetze!

Im grunde muß die Lösung ja ganz einfach sein!

100Messwerte von einem Array in Labels darstellen!! - Mehr nicht!

Liebe Grüße
Tassilo
  • 1
  • 2
  • Page 1 of 2
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   119   133 · Page-Gen-Time: 0.026045s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI