Page 1 of 1

Countdown Timer

Posted: Wed Aug 08, 2007 2:33 pm
by nicksteel
Best way to write simple year month day hour minute second countdown timer to display on screen? Like to display time until graduation, etc.

Just need simple 1 line window like:

2Y:2M:8D:3H:8M:36S

Posted: Wed Aug 08, 2007 3:30 pm
by Trond
Try a TextGadget() together with FormatDate().

Posted: Wed Aug 08, 2007 4:15 pm
by Baldrick
A thread with some timer codes you might be able to use.

http://www.purebasic.fr/english/viewtop ... ht=#181573

Posted: Thu Aug 09, 2007 3:45 pm
by Baldrick
Just been having a bit of a play around with the B3 version & have knocked up a little 24 hr countback timer for you to play with.

Code: Select all


Procedure SelectAll(textfocus)
  SendMessage_(GadgetID(textfocus),#EM_SETSEL,0,-1) 
  SetActiveGadget(textfocus)
EndProcedure

Procedure Update() ; just to update the textgadget in the window only once / second 
 Shared tUpdate.l ;using API timer set @ 1000ms
 tUpdate=1
EndProcedure 

Procedure.s CountBack(CountBackTime$) 
  RawTime.l=ParseDate("%hh%ii%ss", FormatDate("%hh%ii%ss", Date())) ; system time
  CountBackRawTime.l=ParseDate("%hh%ii%ss",CountBackTime$) ;time as set by user
   If RawTime>CountBackRawTime
    RawTime-(ParseDate("%hh%ii%ss","235959")+1)  
   EndIf 
  BackCount.l=CountBackRawTime-RawTime 
 ProcedureReturn FormatDate("%hh hours %ii minutes %ss seconds",BackCount) 
EndProcedure 

Procedure.s TargetString() ; keep time values within spec
 Hr$=GetGadgetText(1) 
  If Val(Hr$)>23
   hr$="23"
   SetGadgetText(1,Hr$)
  EndIf 
 Min$=GetGadgetText(2) 
  If Val(Min$)>59
   Min$="59"
   SetGadgetText(2,Min$)
  EndIf 
 Sec$=GetGadgetText(3) 
  If Val(Sec$)>59
   Sec$="59"
   SetGadgetText(3,Sec$)
  EndIf 
 ProcedureReturn Hr$+Min$+Sec$
EndProcedure 

  If OpenWindow(0,0,0,200,100,"Test",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered) 
   If CreateGadgetList(WindowID(0)) 
    StringGadget(1,10,10,20,20,"21",#PB_String_Numeric) 
    StringGadget(2,35,10,20,20,"59",#PB_String_Numeric)
    StringGadget(3,60,10,20,20,"00",#PB_String_Numeric)
    TextGadget(4,10,40,170,20,"",#PB_Text_Border)
   EndIf 
   eClock=SetTimer_(WindowID(0),2,1000,@Update()) ;once / sec update 
  EndIf 
  
  Repeat 
   Ev.l=WaitWindowEvent() 
    If EventType()=#PB_EventType_Focus
     Select EventGadget() 
      Case 1
       SelectAll(1)
      Case 2 
       SelectAll(2)
      Case 3 
       SelectAll(3)
     EndSelect 
    EndIf 
    If tUpdate.l ; when timer sets value true every 1000ms
     tUpdate=0 
     SetGadgetText(4,CountBack(TargetString())) 
    EndIf 
  Until Ev=#PB_Event_CloseWindow 
   If eClock 
    KillTimer_(WindowID(0),eClock) 
   EndIf