Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Array help (https://www.delphipraxis.net/41496-array-help.html)

delphi_goldy 4. Mär 2005 08:44


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

Muetze1 4. Mär 2005 08:51

Re: Array help
 
Hello!

Why don't you declare a static array and assign the instances of your form at startup?

Delphi-Quellcode:
...

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;
As a small example - it is mostly equal for TEdit, except the type.

Regards
Muetze1

TeronG 4. Mär 2005 09:14

Re: Array help
 
Um das Array zu füllen ....
Delphi-Quellcode:
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;
(spart Tipparbeit :roll:)

delphi_goldy 4. Mär 2005 09:18

Re: Array help
 
Thank You both!

Goldy

Muetze1 4. Mär 2005 09:18

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

Binärbaum 4. Mär 2005 10:19

Re: Array help
 
There is still another way to solve this: create the Edits (or Labels or whatever) dynamically:
Delphi-Quellcode:
var LblArray: array of TLabel;
    i: Integer;
...
  SetLength(LblArr, 4);
  for i:= 0 to High(LblArr) do
    LblArr[i]:= TLabel.Create(nil);
Greets

Binärbaum

Luckie 4. Mär 2005 10:23

Re: Array help
 
Just for completition: Delphi-Referenz durchsuchenFindComponent.


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:49 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz