Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Eine Schleife für die Labels mit Variablenzuordnung(Array)? (https://www.delphipraxis.net/45284-eine-schleife-fuer-die-labels-mit-variablenzuordnung-array.html)

Hallo_Thomas 3. Mai 2005 14:42


Eine Schleife für die Labels mit Variablenzuordnung(Array)?
 
Ich habe jetzt schon eine versuche hintermir, haber wie schaffe ich es unten den Text zsammenzufassen?



die Lange Form
Delphi-Quellcode:
       Form1.Label192.Caption:= IntToStr(Counte[0]) ;
       Form1.Label193.Caption:= IntToStr(Counte[1]) ;
       Form1.Label194.Caption:= IntToStr(Counte[2]) ;
       Form1.Label195.Caption:= IntToStr(Counte[3]) ;
       Form1.Label196.Caption:= IntToStr(Counte[4]) ;
       Form1.Label197.Caption:= IntToStr(Counte[5]) ;
       Form1.Label198.Caption:= IntToStr(Counte[6]) ;
       Form1.Label199.Caption:= IntToStr(Counte[7]) ;
       Form1.Label200.Caption:= IntToStr(Counte[8]) ;
       Form1.Label201.Caption:= IntToStr(Counte[9]) ;
Mein Fehlgeschlagener versuch, da ändert sich im Label gar nix:
Delphi-Quellcode:
begin
 for x:=0 to 9 do
  for i:=192 to 201 do
(FindComponent('form1.Label'+IntToStr(i)) as TLabel).Caption:=IntToStr(Counte[x]);

Waldteufel 3. Mai 2005 14:47

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Hi.

Die innere Schleife wird ja 10x durchlaufen... ;-)

Besser so:

Delphi-Quellcode:
for i:=0 to 9 do
  (FindComponent('Label'+IntToStr(i + 192)) as TLabel).Caption:=IntToStr({ edit: NICHT Counte[x], sondern} Counte[i]);
//edit: So ein blöder Programmierfehler... :wall:

Hallo_Thomas 3. Mai 2005 14:56

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Nein, die Labels


funktionieren auch nicht bei

Delphi-Quellcode:
(FindComponent('form1.Label'+IntToStr(i)) as TLabel).Caption:=IntToStr(Counte[x]);

freak4fun 3. Mai 2005 14:59

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Könnte es vielleicht am Counte[x] liegen? Einfach mal mit i versuchen. Bei mir funktioniert es mit i.

MfG
freak

Hallo_Thomas 3. Mai 2005 15:08

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Also so sieht der Spass ganz aus, wo ich die Labels einzeln angegangen bin(siehe oben) hats geklappt




Delphi-Quellcode:
begin
  for i:=0 to 9 do
  Counte[i]:=0;
  for pl0 := Form1.Anzeige.count-1 DownTo 0 do
    begin
      if StrToInt(Form1.Anzeige.Items[pl0]) in [0,10,20,30] then
      Counte[0] := 0
      else Inc(Counte[0]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [1,11,21,31] then
      Counte[1] := 0
      else Inc(Counte[1]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [2,12,22,32] then
      Counte[2] := 0
      else Inc(Counte[2]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [3,13,23,33] then
      Counte[3] := 0
      else Inc(Counte[3]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [4,14,24,34] then
      Counte[4] := 0
      else Inc(Counte[4]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [5,15,25,35] then
      Counte[5] := 0
      else Inc(Counte[5]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [6,16,26,36] then
      Counte[6] := 0
      else Inc(Counte[6]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [7,17,27] then
      Counte[7] := 0
      else Inc(Counte[7]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [8,18,28] then
      Counte[8] := 0
      else Inc(Counte[8]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [9,19,29] then
      Counte[9] := 0
      else Inc(Counte[9]);
      (FindComponent('form1.Label'+IntToStr(i+192)) as TLabel).Caption:=IntToStr(Counte[i]);
      end;
 end;

@ freak4fun , nee das will auch nicht funken

freak4fun 3. Mai 2005 15:19

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Hier mein Code.

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 1 to 5 do
  (FindComponent('Label'+IntToStr(i)) as TLabel).Caption := IntToStr(47);
end;
Poste doch mal deinen Versuch, mit zusammenfassung.

MfG
freak

Hallo_Thomas 3. Mai 2005 15:24

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
@freak4fun


Du hast rechts einen "stabilen Wert", was bei mir ja nicht der Fall ist, diese Array will nicht harmonieren mit meinem zeugs.

freak4fun 3. Mai 2005 15:30

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Delphi-Quellcode:
      else Inc(Counte[9]);
      (FindComponent('form1.Label'+IntToStr(i+192)) as TLabel).Caption:=IntToStr(Counte[i]);
      end;

Das i scheint mit undefiniert. Das ist kein Schleifenzähler mehr, einfach ein fester Wert.
Was bekommst du denn als ergebnis?

MfG freak

Hallo_Thomas 3. Mai 2005 15:37

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
meinst Du so
Delphi-Quellcode:
begin
  i:=0  ;
  for i:=0 to 9 do
  Counte[i]:=0;

aber wird i nicht durch

Delphi-Quellcode:
for i:=0 to 9 do
definiert?

freak4fun 3. Mai 2005 15:42

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Zitat:

Zitat von Hallo_Thomas
Also so sieht der Spass ganz aus, wo ich die Labels einzeln angegangen bin(siehe oben) hats geklappt




Delphi-Quellcode:
begin
  for i:=0 to 9 do
  Counte[i]:=0;
  for pl0 := Form1.Anzeige.count-1 DownTo 0 do
    begin
      if StrToInt(Form1.Anzeige.Items[pl0]) in [0,10,20,30] then
      Counte[0] := 0
      else Inc(Counte[0]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [1,11,21,31] then
      Counte[1] := 0
      else Inc(Counte[1]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [2,12,22,32] then
      Counte[2] := 0
      else Inc(Counte[2]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [3,13,23,33] then
      Counte[3] := 0
      else Inc(Counte[3]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [4,14,24,34] then
      Counte[4] := 0
      else Inc(Counte[4]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [5,15,25,35] then
      Counte[5] := 0
      else Inc(Counte[5]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [6,16,26,36] then
      Counte[6] := 0
      else Inc(Counte[6]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [7,17,27] then
      Counte[7] := 0
      else Inc(Counte[7]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [8,18,28] then
      Counte[8] := 0
      else Inc(Counte[8]);
      if StrToInt(Form1.Anzeige.Items[pl0]) in [9,19,29] then
      Counte[9] := 0
      else Inc(Counte[9]);
      (FindComponent('form1.Label'+IntToStr(i+192)) as TLabel).Caption:=IntToStr(Counte[i]);
                                            ^
                                     Das i mein ich.        
      end;
 end;

Entschuldige, ich hab mich falsch ausgedrückt. Bei der markierten Stelle(ganz unten), das i ist immer EIN konstannter Wert. Schau es dir mal an. :)

MfG
freak

Khabarakh 3. Mai 2005 16:51

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
1. Wenn du dich schon in einer Methode von TForm1 befindest, kannst du Form1 weglassen. Dann bekommst du auch keine Probleme bei mehreren Instanzen.
2. "Counte": soll das nicht Counter heißen :wink: ?
3. Das lässt sich extrem verkürzen:
Delphi-Quellcode:
var s: string;
begin
  for i := 0 to Anzeige.Count - 1 do
  begin
    s := Anzeige.Items[i];
    case s[Length(s)] of
      '0'..'6':
        if StrToInt(s) < 40 then
          Inc(Counte(StrToInt(s[Length(s)])))
        else
          Counte(StrToInt(s[Length(s)])) := 0;
      '7'..'9':
        if StrToInt(s) < 30 then
          Inc(Counte(StrToInt(s[Length(s)])))
        else
          Counte(StrToInt(s[Length(s)])) := 0;
    end;
  end;
  for i := 0 to 9 do
    (FindComponent('Label'+IntToStr(i+192)) as TLabel).Caption:=IntToStr(Counte[i]);
[edit]So sollte es stimmen:
Delphi-Quellcode:
for i := 0 to Anzeige.Count - 1 do
  begin
    s := Anzeige.Items[i];
    Index := StrToInt(s[Length(s)]);
    for ii := 0 to 9 do
      if (Index = ii) and (Counte[Index] < 30 + Integer(Index <= 6) * 10) then
        Inc(Counte[Index])
      else
        Counte[Index] := 0;
  end;
Gut, das mit dem Boolean zu Integer ist etwas verkünstelt, ich war zu faul, noch ein case einzufügen :wink: .

Hallo_Thomas 3. Mai 2005 17:59

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
@Khabarakh

hab das probiertt, funkt nich

Delphi-Quellcode:
begin
  for i := 0 to Form1.Anzeige.Count - 1 do
  begin
    s := Form1.Anzeige.Items[i];
    case s[Length(s)] of
      '0'..'6':
        if StrToInt(s) < 40 then
          Inc(Counte[StrToInt(s[Length(s)])])
        else
          Counte[StrToInt(s[Length(s)])] := 0;
      '7'..'9':
        if StrToInt(s) < 30 then
          Inc(Counte[StrToInt(s[Length(s)])])
        else
          Counte[StrToInt(s[Length(s)])] := 0;
    end;
  end;
  for i := 0 to 9 do
    (FindComponent('Label'+IntToStr(i+192)) as TLabel).Caption:=IntToStr(Counte[i]);
 end;
hab das probiert funkt nich

Delphi-Quellcode:
var index,ii,i:integer;
begin
for i := 0 to form1.Anzeige.Count - 1 do
  begin
    s := Form1.Anzeige.Items[i];
    Index := StrToInt(s[Length(s)]);
    for ii := 0 to 9 do
      if (Index = ii) and (Counte[Index] < 30 + Integer(Index <= 6) * 10) then
        Inc(Counte[Index])
      else
        Counte[Index] := 0;
        for x:= 0 to 9 do
    (   FindComponent('Label'+IntToStr(x+192)) as TLabel).Caption:=IntToStr(Counte[x]);
  end;
  end;
P.S. können wir uns auf diese Zeile einigen, denn hier muss der Fehler liegen

Delphi-Quellcode:
(   FindComponent('Label'+IntToStr(x+192)) as TLabel).Caption:=IntToStr(Counte[x]);


Delphi-Quellcode:
P.S. hier hieß es nicht Counter weil ich hier den Counter schon als normale Variable vergeben habe

Sharky 3. Mai 2005 18:06

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Zitat:

Zitat von Hallo_Thomas
.... funkt nich

Ist ja auch kein Funkgerät. :roll:

Was funktioniert denn nicht? Kommt eine Fehlermeldung?

Hallo_Thomas 3. Mai 2005 18:25

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Liste der Anhänge anzeigen (Anzahl: 1)
@Sharky es gibt keine Fehlermelung

So hab jetzt mal mein Beispielprogramm so eingestellt das es genau den Fehler macht

Die 10 Zahlen rechts unten sollen die Counter darstellen die eigenlich Zählen müssten

Sharky 3. Mai 2005 19:35

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hai,

warum hast Du denn die Methode Addmyitem bei deiner Form2 deklariert wenn Du sie doch nur in Form1 benutzt.

Das problem liegt aber an dem FindComponent. Es findet die Labels nicht auf der Form1.
Ich habe das ganze mal etwas umgeschrieben.

Schaue Dir vor allem mal mein Set of Byte an ;-)

Hallo_Thomas 3. Mai 2005 20:02

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
@Sharky

Ich danke Dir, bin ja ect begeistert von dem Ergebnis,
Das mit der 2 Unit war eine Fehlgeschlagene Probe, ich wollte sehen ob ich dadurch ´das Programm schneller wird

Sharky 4. Mai 2005 06:53

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Liste der Anhänge anzeigen (Anzahl: 1)
Zitat:

Zitat von Hallo_Thomas
...Ich danke Dir, bin ja ect begeistert von dem Ergebnis,....

Ich habe eben noch einmal den ganzen Code etwas überarbeitet.
  • Nur noch ein OnClick für alle SpeedButtons
  • Optimierung der Funktion AddMyItem
  • Bilder werden nur noch einmal geladen
  • Umsortierung der Methoden wegen der Übersichtlichkeit
Schaue Dir einfach mal an was ich so gemacht habe und beachte die Kommentare im Code ;-)

freak4fun 4. Mai 2005 08:36

Re: Eine Schleife für die Labels mit Variablenzuordnung(Arra
 
Super sharky. :thumb:

MfG
freak


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:03 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