ComponentCount i Pętla

0

Mam na formie 10 Labeli poukładanych w taki sposób

0 1 2 3 4 5 6 7 8 9

ort! je na początek formy takim kodem

For i:=0 to ComponentCount -1 do
if Components[i] is TLabel then
(Components[i] as TLabel).Left:=(Components[i] as TLabel).Left-1;

i teraz chcę żeby kiedy Label osiągnie wartość .Left:=0 żeby wracał na koniec i żeby została do niego dodana wartość 10

Próbowałem Zrobić tak :
{ taki kod jak wyżej plus to poniżej}
if (Components[i] as TLabel).Left=0 then begin {tu sprawdza}
(Components[i] as TLabel).Left:=350; {tu wraca na koniec}
(Components[i] as TLabel).Caption:=inttostr(x); {tu dodaje 10}
end;

wszystko sie ładnie kompiluje ale kiedy to uruchamiam to pojawia się błąd (wszystko mam w timerze) Czy wie ktoś jak można to zapisać żeby Label wracał i powiększał się o 10

Wiem że można tak :

if label1.left=0 then begin
label1.left:=350;
label1.caption:=inttostr(strtoint(label1.Caption)+10)

ale nie perspektywa pisanie 10 razy tego samego troch mnie dołuje

PROSZĘ O POMOC

0

Jaki błąd się pojawia?

0

No wlaśnie, jaki blad, moze masz jeszcze inne labele na formie, a jeszcze dla bezpieczeństwa mozesz zrobic tak:
if (Components[i] as TLabel).Left<=0 then begin
(Components[i] as TLabel).Left:=350-(Components[i] as TLabel).Left;

0

no właśnie BŁĄD jest dość dziwny bo kiedy ostatni Label z numerem '9' dojdzie do zera to program sie zamyka

Jeżeli uruchomie program z poziomu delphi czyli F9 to pojawia się taki komunikat

Project faulted with message 'access violation at 0x77f83350: write of address 0x00030ffc'. Process stopped. Use Step or Run to continiue.

Jeszcze jedno kiedy program działa to naprzemian pokazują się kursory raz zajęty i raz normalny

0

Pozmieniałem nieco kod na taki :

For i:=0 to ComponentCount -1 do begin
if Components[i] is TLabel then
TLabel(Components[i] as TLabel).Left:=TLabel(Components[i] as TLabel).Left-1;
if TLabel(Components[i] as TLabel).Left=0 then begin
TLabel(Components[i] as TLabel).Left:=350;
TLabel(Components[i] as TLabel).Caption:=currtostr(x); {tu dodaje 10}
end;
end;

I teraz pojawia się taki Błąd na dodatek Labele w ogóle się nie przesówają:

<font color="red">Invaild class typecast</span>

0

Poprawny kod:

  For i:=0 to ComponentCount -1 do begin
    if Components[i] is TLabel then begin
      (Components[i] as TLabel).Left:=(Components[i] as TLabel).Left-1;
    if (Components[i] as TLabel).Left=0 then begin
      (Components[i] as TLabel).Left:=350;
      (Components[i] as TLabel).Caption:=currtostr(x);  {tu dodaje 10}
    end;
    end;
  end;

Chyba cosik namieszałem z tymi beginami (chyba jest ich za dużo), ale to na 100% zadziała (sprawdzałem).

0

Wielkie dzięki działa i powiem jedno te begin'y są bardzo potrzebne bez nich po prostu sie sypie

a tu jeszcze podaje powyrzszy kod tylko bez zmiennej 'x':

var
i:integer;
begin
   for i:=0 to ComponentCount -1 do begin
      if Components[i] is TLabel then begin
         (Components[i] as TLabel).Left:=(Components[i] as TLabel).Left-1;
      if (Components[i] as TLabel).Left=0 then begin
         (Components[i] as TLabel).Left:=350;
         (Components[i] as TLabel).Caption:=inttostr(strtoint((Components[i] as TLabel).Caption)+10);
         end;
      end;
   end;
end;

I Jeszcze raz DZIEKI [browar]

1 użytkowników online, w tym zalogowanych: 0, gości: 1