Autor Beitrag
Pinkywesen
Hält's aus hier
Beiträge: 15



BeitragVerfasst: Mi 10.11.10 14:56 
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:

ausblenden volle Höhe 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
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: 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.

_________________
We are, we were and will not be.
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: Mi 10.11.10 19:23 
ausblenden volle Höhe 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!

_________________
Wissenschaft schafft Wissenschaft, denn Wissenschaft ist Wissenschaft, die mit Wissen und Schaffen Wissen schafft. (myself)
Pinkywesen Threadstarter
Hält's aus hier
Beiträge: 15



BeitragVerfasst: Fr 12.11.10 10:02 
So richtig?:

ausblenden volle Höhe 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Fr 12.11.10 10:13 
sleep(??) zwischen die Zeilen, Timerintervall ...

_________________
Das Problem liegt üblicherweise zwischen den Ohren H₂♂
DRY DRY KISS
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: 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.

_________________
We are, we were and will not be.
Lossy eX
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1048
Erhaltene Danke: 4



BeitragVerfasst: 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));

_________________
Nur die Menschheit ist arrogant genug, um zu glauben sie sei die einzige intelligente Lebensform im All. Wo nicht mal das nachhaltig bewiesen wurde.

Für diesen Beitrag haben gedankt: Pinkywesen
bummi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Fr 12.11.10 16:39 
oouupps, die Schleife hatte ich gar nicht gesehen, dachte läuft alles über den Timer.

_________________
Das Problem liegt üblicherweise zwischen den Ohren H₂♂
DRY DRY KISS
Holg_i
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Do 18.11.10 10:16 
ausblenden 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; // alle 100ms. eventuell verändern.
     Timer1.enabled:=True; // Timer wird eingeschaltet.
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
     Label1.Caption :=IntToStr(random(99)); // Warum auch immer du deine Zufallszahl vorab in der Variable Zahl abspeicherst.
     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; // wo zählst du die Schleife höher????
     inc(Schleife); // Solltest du die Schleife nirgens höher zählen so wäre hier ein klasse Punkt. Die Variable Schleife vorab in Privat deklarieren.
end;
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Do 18.11.10 10:43 
Du meinst sicher Timer1.enabled := false; statt Timer1.Intervall := false;
Holg_i
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Do 18.11.10 12:36 
user profile iconF34r0fTh3D4rk hat folgendes geschrieben Zum zitierten Posting springen:
Du meinst sicher Timer1.enabled := false; statt Timer1.Intervall := false;


Wo Du Recht hast hast Du Recht