Recent

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

OC DelGuy

  • New Member
  • *
  • Posts: 32
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.0.10
Date: 07 JUL 2020
FPC Version: 3.2.0
SVN Revision: 63526
x86_64-win64-win32/win64

dbannon

  • Hero Member
  • *****
  • Posts: 1474
    • 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 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

OC DelGuy

  • New Member
  • *
  • Posts: 32
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.0.10
Date: 07 JUL 2020
FPC Version: 3.2.0
SVN Revision: 63526
x86_64-win64-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 4732
Re: When does Label.caption update?
« Reply #3 on: June 17, 2021, 02:21:11 am »
Or MYLabel.Repaint;

which maybe better to avoid clashes with messages in the que..
The only true wisdom is knowing you know nothing

dbannon

  • Hero Member
  • *****
  • Posts: 1474
    • tomboy-ng, a rewrite of the classic Tomboy
Re: When does Label.caption update?
« Reply #4 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 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 952
    • Lebeau Software
Re: When does Label.caption update?
« Reply #5 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