Recent

Author Topic: Weird issue updating a label caption  (Read 2084 times)

Citizen Bleys

  • Newbie
  • Posts: 2
Weird issue updating a label caption
« on: October 31, 2018, 03:24:38 pm »
I just started with Lazarus; my last attempt to use Pascal was in the 90s writing little DOS apps so I'm incredibly rusty and have never done a GUI application before (unless you count web development)

I'm trying to make a button that displays a 15 minute countdown timer in real time.  It should be simple, right?

Code: Pascal  [Select][+][-]
  1. procedure TSWF1.buBreakClick(Sender: TObject);
  2.  
  3. begin
  4.    target := IncMinute(Now,15);
  5.    While (Now <= target) do
  6.        begin
  7.             cdtimer.caption := FormatDateTime('mm:ss:z',target - Now);
  8.             sleep(10); {wait 10 ms}
  9.             {showmessage('looped'); }
  10.        end;
  11. end;                          
  12.  

See that commented out showmessage?  If I don't comment it out, every time the OK button is pressed, it updates the label with the incorrect time (starts out showing 12 minutes 00 seconds, then goes to 12 mins 59 seconds, then counts down as normal)
If there's no showmessage, the label just never updates.

Ideally, I'd also like to show the first 2 digits of the miliseconds and drop the third, but no matter how many z's I put in the format field it always shows 3.

I've tried everything I can think of, including manually making integer variables for minutes, seconds, and miliseconds and manually altering then every loop, but that still won't display without a showmessage.  I've even tried saying all of the magic words - my co-workers now believe I am transforming into Samuel L. Jackson.

How can I get this button to show a countdown timer without the user clicking an OK button every 10ms?

EDIT: target is a TDateTime declared in the form's private variables.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Weird issue updating a label caption
« Reply #1 on: October 31, 2018, 03:57:56 pm »
minutes are 'nn' in formatstrings   'mm' is months.


ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Weird issue updating a label caption
« Reply #2 on: October 31, 2018, 04:32:17 pm »
minutes are 'nn' in formatstrings   'mm' is months.
More precisely in the documentation: "month or minutes if preceded by h or hh specifiers".

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: Weird issue updating a label caption
« Reply #3 on: October 31, 2018, 04:38:17 pm »
Use a Timer component.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Weird issue updating a label caption
« Reply #4 on: October 31, 2018, 05:14:36 pm »
use a TTimer set for one second intervals for example..

In the OnTime event you can then update your caption or what ever.

When you initiate the timer, start it via a button or what ever, store the current time somewhere so you can then
subtract the new time to get your seconds.

for example in your initiation of the timer run:

 SomeVariableSomeWhere  := Now; // gets current time.

 
 and in the timer event:

 Caption := FormatDatexxx( Now-SomeVariableSomeWhere);

I think you get it.

P.S.
 Remember to stop it when it gets to where you want to be and if you are doing a count down then you need to add your preset to the
first Now and then subtract the Current NOW until you get a - number, at which point you stop the timer.

etc.

 If you can't figure that out I am sure one of us can offer a code block sample.
The only true wisdom is knowing you know nothing

Citizen Bleys

  • Newbie
  • Posts: 2
Re: Weird issue updating a label caption
« Reply #5 on: October 31, 2018, 07:57:45 pm »
Thanks, the nn solved the fact that all timers were 13 minute timers.  The initial issue was resolved with a "Application.Processmessages;" statement, now it does what I want it to (besides the fact that I can't add an Always On Top option to the system menu, as every example I can find is a delphi example that calls object types that don't exist in freepascal)

 

TinyPortal © 2005-2018