AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Array help

Ein Thema von delphi_goldy · begonnen am 4. Mär 2005 · letzter Beitrag vom 4. Mär 2005
Antwort Antwort
delphi_goldy

Registriert seit: 28. Feb 2005
Ort: Celje
8 Beiträge
 
Delphi 7 Professional
 
#1

Array help

  Alt 4. Mär 2005, 08:44
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
  Mit Zitat antworten Zitat
Muetze1
(Gast)

n/a Beiträge
 
#2

Re: Array help

  Alt 4. Mär 2005, 08:51
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
  Mit Zitat antworten Zitat
Benutzerbild von TeronG
TeronG

Registriert seit: 19. Jul 2004
Ort: München
960 Beiträge
 
Delphi 2007 Professional
 
#3

Re: Array help

  Alt 4. Mär 2005, 09:14
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 )
  Mit Zitat antworten Zitat
delphi_goldy

Registriert seit: 28. Feb 2005
Ort: Celje
8 Beiträge
 
Delphi 7 Professional
 
#4

Re: Array help

  Alt 4. Mär 2005, 09:18
Thank You both!

Goldy
  Mit Zitat antworten Zitat
Muetze1
(Gast)

n/a Beiträge
 
#5

Re: Array help

  Alt 4. Mär 2005, 09:18
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
  Mit Zitat antworten Zitat
Benutzerbild von Binärbaum
Binärbaum

Registriert seit: 19. Jan 2005
Ort: Elstra
764 Beiträge
 
Delphi 7 Enterprise
 
#6

Re: Array help

  Alt 4. Mär 2005, 10:19
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
There are exactly 10 kinds of people: those who understand binary, and those who don't.
---
"Software reift beim Kunden. Bei Hardware ist es anders: Hardware fault beim Kunden." - Rainer G. Spallek
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#7

Re: Array help

  Alt 4. Mär 2005, 10:23
Just for completition: Delphi-Referenz durchsuchenFindComponent.
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:01 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