![]() |
Array help
Hi!
Unfortunately I don't speak German very well :( , so I'll ask for help in English! I would like to know whether is possible to use objects (like Edit, Label, etc.) like array (like in Visual Basic): Edit[1], Edit[24], etc? Same name, different Index. I don't care if answer comes in English or German (I speak that much German :) ). Goldy |
Re: Array help
Hello!
Why don't you declare a static array and assign the instances of your form at startup?
Delphi-Quellcode:
As a small example - it is mostly equal for TEdit, except the type.
...
Type TForm1 = Class(TForm) Label1 : TLabel; Label2 : TLabel; Label3 : TLabel; Label4 : TLabel; .... Procedure FormCreate(Sender: TObject); Procedure Button1Click(Sender: TObject); Private Labels : Array[1..4] Of TLabel; End; .... Procedure TForm1.FormCreate(Sender: TObject); Begin Labels[1] := Label1; Labels[2] := Label2; Labels[3] := Label3; Labels[4] := Label4; End; Procedure TForm1.Button1Click(Sender: TObject); Var i : Integer; Begin For i := Low(Labels) To High(Labels) Do Labels[i].Visible := False; End; Regards Muetze1 |
Re: Array help
Um das Array zu füllen ....
Delphi-Quellcode:
(spart Tipparbeit :roll:)
procedure TForm1.FormCreate(Sender: TObject);
var i : Integer; begin For i := Low(Labels) To High(Labels) Do Labels[i] := TLabel(FindComponent('Label' + IntToStr(i))); // so oder // Labels[i] := (FindComponent ('Label'+inttostr(i)) as TLabel); // so end; |
Re: Array help
Thank You both!
Goldy |
Re: Array help
Hello!
Yes, maybe it is shorter, but let us assume that you've delete Label3: - my code will fail to compile - your code will compile and run - but it will raise later on an exception because the access to a Nil element My code assures you, that all elements exists and can be accessed. Regards, Muetze1 |
Re: Array help
There is still another way to solve this: create the Edits (or Labels or whatever) dynamically:
Delphi-Quellcode:
Greets
var LblArray: array of TLabel;
i: Integer; ... SetLength(LblArr, 4); for i:= 0 to High(LblArr) do LblArr[i]:= TLabel.Create(nil); Binärbaum |
Re: Array help
Just for completition:
![]() |
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:04 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz