Recent

Author Topic: When does Label.caption update?  (Read 4245 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
When does Label.caption update?
« on: June 17, 2021, 02:01:17 am »
Hi y'all!
Ok, so I wrote this little procedure to give a preview of some questions that will appear later on in the program and their specific category codes.

Ques is a TLabel and Start is a TButton.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StartClick(Sender: TObject);
  2. begin
  3.   For x:=1 to 24 do
  4.     Begin
  5.       Ques.caption:= Questions[x] +'   '+QuestionCats[x];
  6.       Sleep(500);
  7.     end;
  8. end;  
The problem is that when I click the button, some time goes by and then the LAST question with it's appropriate code appears.
I would have thought that the first question and its code appears and the program waits a bit, then the second question and its code appears and it waits until the last question.  Why doesn't the new label caption appear each iteration of the for loop?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: When does Label.caption update?
« Reply #1 on: June 17, 2021, 02:07:52 am »
Lazarus updates screen content "when it gets around to it" - things like that are put in a queue and done when the system has some time on its hands.

Call Application.ProcessMessages; just before the sleep command to ensure the queue is processed when you want it done.

Davo
 
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
Re: When does Label.caption update?
« Reply #2 on: June 17, 2021, 02:12:18 am »
Yep, That did it!  Thanks Davo!
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: When does Label.caption update?
« Reply #3 on: June 17, 2021, 09:02:36 am »
Yes, repaint would be a bit (or a lot) less forceful.  I tend to think of Application.ProcessMessages as a general solution to that sort of problem. OC DelGuy, I'd try Jamie's way if I was you ...

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: When does Label.caption update?
« Reply #4 on: June 17, 2021, 06:34:54 pm »
Or MYLabel.Repaint;

Or MYLabel.Update;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018