Entwickler-Ecke
Grafische Benutzeroberflächen (VCL & FireMonkey) - Prozedur dauerhaft laufen lassen
Pinkywesen - Mi 10.11.10 14:56
Titel: Prozedur dauerhaft laufen lassen
Guten Tag liebe Member von Delphi-Forum.de,
ich möchte gerne mein erstes Projekt ein bisschen verschönern und eine kleine "Animation" laufen lassen. Animation ist schon übertrieben da ich nur möchte das sich ca. 10 Labels dauerhaft mit zufälligen Zahlen füllen. So das das ein bisschen aussieht wie eine Matrix. Dazu habe ich folgenden Source Code:
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42:
| var Form1: TForm1; zahl: integer; const Schleife = 1; Schleife1 = 0;
implementation
{$R *.DFM}
procedure TForm1.FormActivate(Sender: TObject); LABEL anfang; begin anfang: Label1.Caption := IntToStr(zahl); zahl := random(99); Label2.Caption := IntToStr(zahl); zahl := random(99); Label3.Caption := IntToStr(zahl); zahl := random(99); Label4.Caption := IntToStr(zahl); zahl := random(99); Label5.Caption := IntToStr(zahl); zahl := random(99); Label6.Caption := IntToStr(zahl); zahl := random(99); Label7.Caption := IntToStr(zahl); zahl := random(99); Label8.Caption := IntToStr(zahl); zahl := random(99); Label9.Caption := IntToStr(zahl); zahl := random(99); Label10.Caption := IntToStr(zahl); zahl := random(99); Label11.Caption := IntToStr(zahl); zahl := random(99); Label12.Caption := IntToStr(zahl); zahl := random(99); if (schleife <10) then goto anfang; end; end. |
Ich weiß das der Code noch nicht sauber ist und auch nicht gut aussieht, aber ich möchte ja erstmal eine funktionierende Schleife. Jedenfalls hängt sich der gezeigte Code sofort auf und nichts geht mehr.
Habt ihr eine Lösung für mein Problem?
Gausi - Mi 10.11.10 15:03
Soso, Delphi-Projekt verschönern mit einer GoTo-Schleife. Das wird iirc mit einem 2-Minuten-Bad in flüssigem Blei bestraft. ;-)
Erstens: Das kann man mit einer normalen Schleife genauso gut machen.
Zweitens: Deine Schleife ist eine Endlosschleife, da deine Variable Schleife nie verändert wird.
Drittens: Deine Schleife blockiert das Programm, da die Prozedur läuft und läuft und läuft und das Programm keine Möglichkeit hat, auf Messages zu reagieren (Mausklick, Tasten, etc.)
Lösung: Schau dir mal die TTimer-Komponente an. Ins OnTimer-Event kommt dein Code, aber ohne die Schleife drumherum, und ohne GoTo.
platzwart - Mi 10.11.10 19:23
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42:
| var Form1: TForm1; zahl: integer; const Schleife = 1; Schleife1 = 0;
implementation
{$R *.DFM}
procedure TForm1.FormActivate(Sender: TObject); begin Repeat Label1.Caption := IntToStr(zahl); zahl := random(99); Label2.Caption := IntToStr(zahl); zahl := random(99); Label3.Caption := IntToStr(zahl); zahl := random(99); Label4.Caption := IntToStr(zahl); zahl := random(99); Label5.Caption := IntToStr(zahl); zahl := random(99); Label6.Caption := IntToStr(zahl); zahl := random(99); Label7.Caption := IntToStr(zahl); zahl := random(99); Label8.Caption := IntToStr(zahl); zahl := random(99); Label9.Caption := IntToStr(zahl); zahl := random(99); Label10.Caption := IntToStr(zahl); zahl := random(99); Label11.Caption := IntToStr(zahl); zahl := random(99); Label12.Caption := IntToStr(zahl); zahl := random(99); Application.ProcessMessages; Until(schleife >= 10); end; end. |
Aber wie bereits beschrieben, löse das mit einem TTimer!
Pinkywesen - Fr 12.11.10 10:02
So richtig?:
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
| procedure TForm1.FormActivate(Sender: TObject); begin Timer1.OnTimer(self); end;
procedure TForm1.Timer1Timer(Sender: TObject); begin Repeat Label1.Caption := IntToStr(zahl); zahl := random(99); Label2.Caption := IntToStr(zahl); zahl := random(99); Label3.Caption := IntToStr(zahl); zahl := random(99); Label4.Caption := IntToStr(zahl); zahl := random(99); Label5.Caption := IntToStr(zahl); zahl := random(99); Label6.Caption := IntToStr(zahl); zahl := random(99); Label7.Caption := IntToStr(zahl); zahl := random(99); Label8.Caption := IntToStr(zahl); zahl := random(99); Label9.Caption := IntToStr(zahl); zahl := random(99); Label10.Caption := IntToStr(zahl); zahl := random(99); Label11.Caption := IntToStr(zahl); zahl := random(99); Label12.Caption := IntToStr(zahl); zahl := random(99); Label13.Caption := IntToStr(zahl); zahl := random(99); Label14.Caption := IntToStr(zahl); zahl := random(99); Application.ProcessMessages; Until(schleife >= 10); end;
end. |
Und wie kann ich die Prozedur langsamer ausführen lassen?
bummi - Fr 12.11.10 10:13
sleep(??) zwischen die Zeilen, Timerintervall ...
Gausi - Fr 12.11.10 10:24
Nein. Komplett unsinnig.
Der Timer ist dafür da, eine bestimmte Aktion alle X Millisekunden erneut auszuführen, solange der Timer Enabled ist. Im OnTimer-Event eine Endlosschleife zu bauen ist Quatsch. Lass die Schleife da einfach weg, stell das Intervall auf 100ms und fertig. Das FormActivate kannst du auch weglassen.
Lossy eX - Fr 12.11.10 11:45
Pinkywesen: Da Gausi zur Schleife eigentlich schon alles gesagt hat werde ich dazu nix weiter sagen. Du hast aber in deinem Code noch einen logikdreher drin. Und zwar machst du folgendes. Zu erst weißt du einem Label den Inhalt von Zahl zu und anschließend generierst du eine zufällige Zahl und packst diese in die variable Zahl. Na fällt dir was auf? Das erste mal, wenn Label1 zugewiesen wird ist der Wert 0, da Zahl noch keinen Inhalt hat und ansonsten weißt du immer dem nachfolgenden Label den erzeugten Wert zu. Denn ich denke nicht, dass das wirklich so gewollt ist. Der letzte Wert überträgt sich deswegen nur auf das erste Label, weil du dafür eine globale Variable benutzt. Du könntest in dem Fall eigentlich auch direkt so etwas machen wie Label1.Caption := IntToStr(Random(99));
bummi - Fr 12.11.10 16:39
oouupps, die Schleife hatte ich gar nicht gesehen, dachte läuft alles über den Timer.
Holg_i - Do 18.11.10 10:16
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
| procedure TForm1.FormActivate(Sender: TObject); begin Schleife:=0; Timer1.Intervall=100; Timer1.enabled:=True; end;
procedure TForm1.Timer1Timer(Sender: TObject); begin Label1.Caption :=IntToStr(random(99)); Label2.Caption :=IntToStr(random(99)); Label3.Caption :=IntToStr(random(99)); Label4.Caption :=IntToStr(random(99)); Label5.Caption :=IntToStr(random(99)); Label6.Caption :=IntToStr(random(99)); Label7.Caption :=IntToStr(random(99)); Label8.Caption :=IntToStr(random(99)); Label9.Caption :=IntToStr(random(99)); Label10.Caption :=IntToStr(random(99)); Label11.Caption :=IntToStr(random(99)); Label12.Caption :=IntToStr(random(99)); Label13.Caption :=IntToStr(random(99)); Label14.Caption :=IntToStr(random(99)); IF Schleife>=10 then Timer1.Intervall:=false; inc(Schleife); end; |
F34r0fTh3D4rk - Do 18.11.10 10:43
Du meinst sicher Timer1.enabled := false; statt Timer1.Intervall := false;
Holg_i - Do 18.11.10 12:36
F34r0fTh3D4rk hat folgendes geschrieben : |
| Du meinst sicher Timer1.enabled := false; statt Timer1.Intervall := false; |
Wo Du Recht hast hast Du Recht
Entwickler-Ecke.de based on phpBB
Copyright 2002 - 2011 by Tino Teuber, Copyright 2011 - 2026 by Christian Stelzmann Alle Rechte vorbehalten.
Alle Beiträge stammen von dritten Personen und dürfen geltendes Recht nicht verletzen.
Entwickler-Ecke und die zugehörigen Webseiten distanzieren sich ausdrücklich von Fremdinhalten jeglicher Art!