Put time on the caption of the main form

Just starting out? Need help? Post your questions and find answers here.
PowerSoft
User
User
Posts: 65
Joined: Sun Aug 16, 2015 2:54 pm

Put time on the caption of the main form

Post by PowerSoft »

How do I put the actual time in the caption of my main form?
Thanks for any help :lol:

Code: Select all

; The main event loop as usual, the only change is to call the automatically
; generated event procedure for each window.
  Repeat
    Event = WaitWindowEvent()
    
    Select EventWindow()
      Case #FrmMainGenerator  ; button generate gedrukt
        FrmMain_Events(event) ; execute form main events
      Case #FrmMainCopy       ; button copy gedrukt
        FrmMain_Events(event) ; execute form main events 
    EndSelect
  PutTimeInCaption()
  Until Event = #PB_Event_CloseWindow ; Quit on any window close


  Procedure PutTimeInCaption()
    time$=FormatDate("%mm/%dd/%yyyy", Date()) +"  "+FormatDate("%hh:%ii:%ss", Date())
    ?????????
  EndProcedure
OS X 10.10.5 PB 5.31(x64)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Put time on the caption of the main form

Post by Kwai chang caine »

Perhaps like this

Code: Select all

Enumeration
 #FrmMainGenerator
 #FrmMainCopy
EndEnumeration

Procedure PutTimeInCaption()
 time$=FormatDate("%mm/%dd/%yyyy", Date()) +"  "+FormatDate("%hh:%ii:%ss", Date())
 SetWindowTitle(#FrmMainGenerator, Time$)
EndProcedure

Procedure  FrmMain_Events(event)
EndProcedure 

OpenWindow(#FrmMainGenerator, 100, 100,500, 100, "Window")

Repeat
 Event = WaitWindowEvent(10)
 
 Select EventWindow()
  Case #FrmMainGenerator , #FrmMainCopy
   FrmMain_Events(event) ; execute form main events
   
 EndSelect
 PutTimeInCaption()
Until Event = #PB_Event_CloseWindow ; Quit on any window close


ImageThe happiness is a road...
Not a destination
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Put time on the caption of the main form

Post by Bisonte »

Code: Select all

????????? = SetWindowTitle(Window, time$)
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Put time on the caption of the main form

Post by ts-soft »

Code: Select all

Procedure EventSetTitle()
  SetWindowTitle(0, FormatDate("%mm/%dd/%yyyy  %hh:%ii:%ss", Date()))
EndProcedure

OpenWindow(0, #PB_Ignore, #PB_Ignore, 640, 20, "")
AddWindowTimer(0, 1, 250)
BindEvent(#PB_Event_Timer, @EventSetTitle(), 0, 1)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
?
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Put time on the caption of the main form

Post by TI-994A »

PowerSoft wrote:How do I put the actual time in the caption of my main form?

Code: Select all

; The main event loop as usual, the only change is to call the automatically
; generated event procedure for each window.
A multi-window approach:

Code: Select all

Global windows

Procedure UpdateTime()
  Protected win = EventWindow()
  SetWindowTitle(win, "Window " + Str(GetWindowData(win)) + " - " + 
                      FormatDate("%mm/%dd/%yyyy  %hh:%ii:%ss", Date()))
EndProcedure

Procedure CloseWin()
  CloseWindow(EventWindow())
  windows - 1
EndProcedure

Procedure AddTime(win.i)
  Static nextTimer
  If IsWindow(win)
    nextTimer + 1
    windows + 1
    AddWindowTimer(win, nextTimer, 1000)
    BindEvent(#PB_Event_Timer, @UpdateTime())
    BindEvent(#PB_Event_CloseWindow, @CloseWin(), win)
    SetWindowData(win, nextTimer)
  EndIf  
EndProcedure

win1 = OpenWindow(#PB_Any, 200, 200, 600, 300, "Window 1")
win2 = OpenWindow(#PB_Any, 300, 300, 600, 300, "Window 2")
win3 = OpenWindow(#PB_Any, 400, 400, 600, 300, "Window 3")

AddTime(win1)
AddTime(win2)
AddTime(win3)

While WaitWindowEvent() ! #PB_Event_CloseWindow And windows : Wend
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
PowerSoft
User
User
Posts: 65
Joined: Sun Aug 16, 2015 2:54 pm

Re: Put time on the caption of the main form

Post by PowerSoft »

Kwai chang caine wrote:Perhaps like this

Code: Select all

Enumeration
 #FrmMainGenerator
 #FrmMainCopy
EndEnumeration

Procedure PutTimeInCaption()
 time$=FormatDate("%mm/%dd/%yyyy", Date()) +"  "+FormatDate("%hh:%ii:%ss", Date())
 SetWindowTitle(#FrmMainGenerator, Time$)
EndProcedure

Procedure  FrmMain_Events(event)
EndProcedure 

OpenWindow(#FrmMainGenerator, 100, 100,500, 100, "Window")

Repeat
 Event = WaitWindowEvent(10)
 
 Select EventWindow()
  Case #FrmMainGenerator , #FrmMainCopy
   FrmMain_Events(event) ; execute form main events
   
 EndSelect
 PutTimeInCaption()
Until Event = #PB_Event_CloseWindow ; Quit on any window close


The time is printed in the main window, but is not automaticly updated. :(
Only when there are activities!
Like when the mouse is going over the window
OS X 10.10.5 PB 5.31(x64)
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Put time on the caption of the main form

Post by Marc56us »

As ts-soft and TI-994A said: uses a Timer to force the execution of a procedure at a given interval.
http://www.purebasic.com/documentation/ ... timer.html

(your sample with timer)
I deleted the procedure because one line is enough to update the title of the window.
I am very lazy :mrgreen:

Code: Select all

Enumeration
     #FrmMainGenerator
     #FrmMainCopy
     #Sec
EndEnumeration

; Note needed, see below
; Procedure PutTimeInCaption()
;      time$=FormatDate("%mm/%dd/%yyyy", Date()) +"  "+FormatDate("%hh:%ii:%ss", Date())
;      SetWindowTitle(#FrmMainGenerator, Time$)
; EndProcedure

Procedure  FrmMain_Events(event)
EndProcedure

OpenWindow(#FrmMainGenerator, 100, 100,500, 100, "Window")
; --- Add timer (1sec)
AddWindowTimer(#FrmMainGenerator, #Sec, 1000)

Repeat
     Event = WaitWindowEvent(10)
     
     If Event = #PB_Event_Timer
          SetWindowTitle(#FrmMainGenerator, 
                         FormatDate("%mm/%dd/%yyyy %hh:%ii:%ss", 
                                    Date()))     
     EndIf
     
     Select EventWindow()
  
          Case #FrmMainGenerator, #FrmMainCopy
               FrmMain_Events(event) ; execute form main events
               
     EndSelect
     ;PutTimeInCaption()
     
Until Event = #PB_Event_CloseWindow ; Quit on any window close
:wink:
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Put time on the caption of the main form

Post by ts-soft »

@Marc56us

But when you move the window, your time is stopping. Better is to use BindEvent()!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Put time on the caption of the main form

Post by TI-994A »

PowerSoft wrote:The time is printed in the main window, but is not automaticly updated.
Here's a simplified, single-window version:

Code: Select all

Procedure UpdateTime()
  SetWindowTitle(win, "Window Title - " + 
                      FormatDate("%mm/%dd/%yyyy %hh:%ii:%ss", Date()))
EndProcedure

OpenWindow(0, 300, 300, 600, 300, "Window Title")
AddWindowTimer(0, 0, 1000)
BindEvent(#PB_Event_Timer, @UpdateTime())

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply