Autor Beitrag
Halunkel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34



BeitragVerfasst: So 21.05.06 01:39 
Hallo!

Wie kann ich denn mit einer Schleife in folgendem Fall viel Schreibarbeit sparen??

Ich würde gerne viele Labels (und andere Dinge) einblenden, wenn eine gewisse Auswahl in einer Combobox gewählt wurde.

Die Labels heißen Label1....Label100 und so würde ich nur ungerne schreiben müssen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
if combobox1.Text = 'Eintrag1' then
  begin
    Label1.visible := true;
    Label2.visible := true;
         .
         .
         .
    Label100.visible := true;


Da gibt es doch sicher ne Möglichkeit das etwas eleganter und übersichtlicher zu fassen mit ner for... to... Schleife oder?? Aber wie bekomme ich die Variable dann in den Label-Begriff?? Ich hätte es mir etwa so vorgestellt, aber das wäre auch zu einfach gewesen...
ausblenden Delphi-Quelltext
1:
2:
3:
4:
if combobox1.Text = 'Eintrag1' then
  begin
    for i:= 1 to 5 do
      Label(i).visible := true; {funktioniert leider nicht}


Dann hab ich noch was gelesen von FindComponent, aber das klappt leider auch nicht in etwa so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
if combobox1.Text = 'Eintrag1' then
  begin
    for i:= 1 to 5 do
      TLabel(FindComponent('Label'+IntToStr(i)+'.visible')) := true; {funktioniert leider nicht}


Gibts hierfür ne Lösung??
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: So 21.05.06 09:16 
War ja fast richtig, hättest Du deinen obigen Code mal mit dem unteren verglichen wärest Du wahrscheinlich noch draufgekommen wo der Fehler lag.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.Button1Click(Sender: TObject);
var
 i:Integer;
begin
if combobox1.Text = 'Eintrag1' then
  begin
    for i:= 1 to 5 do
      TLabel(FindComponent('Label'+IntToStr(i))).visible := True;
  end;
end;
Jetstream
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 222



BeitragVerfasst: So 21.05.06 11:23 
Du hast 100 Labels per Hand erstellt ? RESPEKT !! :D

kannst dir auch einfach 100 Labels in einem Array dynamisch erzeugen, das geht wahrscheinlich einfacher und die Klickarbeit fällt weg.

geht irgendwie so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
Feld:Array[1..100of TLabel;
a:integer;

for a:=1 to 100 do Feld[a].visible:=true;

Da ich mich grade mit Java rumschlage, kann die Syntax auch vollkommen falsch sein :roll:
Und die Labels sollten noch Eigenschaften zugewiesen bekommen.
Jakob Schöttl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Mo 22.05.06 12:00 
Ich hab das mal so gemacht:

ausblenden Delphi-Quelltext
1:
2:
  for i := 1 to 5 do
    (FindComponent('Label' + IntToStr(i)) as TLabel).Visible := False;


So hats bei mir damals glaub ich geklappt! Probiers aus.