none
Virtual Clock

    Question

  • The following code creates a virtual clock (i.e one where the time is not the system time).

    Code works fine except when a single digit is displayed.  I want single digits to show as e.g. 07 not 7 but cannot find a way to do this and am hoping that someone else may already have solved this issue.

    GraphicsWindow.Width = 500
    GraphicsWindow.Height = 250
    GraphicsWindow.Left = (Desktop.Width / 2) - (GraphicsWindow.Width / 2)
    GraphicsWindow.Top = (Desktop.Height / 2) - (GraphicsWindow.Height / 2)
    GraphicsWindow.CanResize = "False"
    GraphicsWindow.Title = "Small Basic Virtual Clock"
    GraphicsWindow.BackgroundColor = "Black"
    GraphicsWindow.FontSize = 30
    GraphicsWindow.BrushColor = "Green"
    GraphicsWindow.FontName = "Courier New"
    virtualHour = "08"
    virtualMin = "00"
    virtualSec = "00"
    GraphicsWindow.DrawText(50, 50, virtualHour + ":" + virtualMin + ":" + virtualSec) ' To show what clock should look like when running

    Timer.Interval = 1000 ' Decrease this to see clock tick over faster
    Timer.Tick = clockAdvance

    Sub clockAdvance
      virtualSec = virtualSec + 1
      If virtualSec = 60 Then
        virtualSec = virtualSec - 60
        virtualMin = virtualMin + 1
      ElseIf virtualMin = 60 Then
        virtualMin = virtualMin - 60
        virtualHour = virtualHour + 1
      Elseif virtualHour = 24 Then
       virtualHour = 00
     EndIf
     
      GraphicsWindow.Clear()
      GraphicsWindow.DrawText(50, 100, virtualHour + ":")
      GraphicsWindow.DrawText(100, 100, virtualMin + ":")
      GraphicsWindow.DrawText(150, 100, virtualSec)
     
    EndSub

    Chris

    Wednesday, March 15, 2017 2:50 AM

Answers

  • And a rather obtuse but one line way.

    Add a zero at the start and take the last 2 characters.

      virtualSec1 = Text.GetSubText(Text.Append("0",virtualSec),Text.GetLength(virtualSec),2)
      virtualMin1 = Text.GetSubText(Text.Append("0",virtualMin),Text.GetLength(virtualMin),2)
      virtualHour1 = Text.GetSubText(Text.Append("0",virtualHour),Text.GetLength(virtualHour),2)
      GraphicsWindow.DrawText(50, 100, virtualHour1+":"+virtualMin1+":"+virtualSec1)



    Wednesday, March 15, 2017 8:22 PM
    Moderator

All replies

  • Use 
    If virtualSec<10 then
      GraphicsWindow.DrawText(150,100,Text.Append("0",virtualSec))
    Else
      GraphicsWindow.DrawText(150, 100, virtualSec)
    EndIf
    And the equivalent for MIn and Hour

    Jan [ WhTurner ] The Netherlands

    Wednesday, March 15, 2017 10:18 AM
    Answerer
  • And a rather obtuse but one line way.

    Add a zero at the start and take the last 2 characters.

      virtualSec1 = Text.GetSubText(Text.Append("0",virtualSec),Text.GetLength(virtualSec),2)
      virtualMin1 = Text.GetSubText(Text.Append("0",virtualMin),Text.GetLength(virtualMin),2)
      virtualHour1 = Text.GetSubText(Text.Append("0",virtualHour),Text.GetLength(virtualHour),2)
      GraphicsWindow.DrawText(50, 100, virtualHour1+":"+virtualMin1+":"+virtualSec1)



    Wednesday, March 15, 2017 8:22 PM
    Moderator