In your answer to my previously posted topic, “Changing label captions in FOR loop,” you suggested I use “Sleep.” When I used it, the loop slowed down but didn’t display anything on my form. It looks like the number i increases by one and sleeps for 200 milliseconds again and again until i:= 1000 then the label.caption changes to i:= 1000. What’s wrong?
The Sleep was inserted intentionally to slow things down, but you don’t need to do that. I’m guessing that what you’re not doing is allowing the program to receive messages, which typically occurs with loops.
For instance, the following code will do the counting, but won’t do anything to change the display:
procedure TForm1.Button1Click(Sender: TObject);var I : Integer;begin for I := 1 to 1000 do Label1.Caption := IntToStr(I);end;
So what do you need to do? Well, you have to allow the program to continue to receive messages from Windows even though it’s busy with a loop. This is called “yield,” where the program momentarily yields what it’s doing to check its message queue. The following code will affect that behavior:
procedure TForm1.Button1Click(Sender: TObject);var I : Integer;begin for I := 1 to 1000 do begin Label1.Caption := IntToStr(I); Application.ProcessMessages; end;end;
You should now be able to see the display with this code quite easily.