Create a simple timer to count seconds, minutes and hours

后端 未结 4 957
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 15:47

I\'m trying to create a pretty simple program that basically is a timer. I have three sets of labels, lbl_seconds, lbl_minutes and lbl_hours

4条回答
  •  半阙折子戏
    2021-01-07 16:12

    I think you need something of this sort

    Public Function GetTime(Time as Integer) As String
        Dim Hrs        As Integer  'number of hours   '
        Dim Min        As Integer  'number of Minutes '
        Dim Sec        As Integer  'number of Sec     '
    
        'Seconds'
        Sec = Time Mod 60
    
        'Minutes'
        Min = ((Time - Sec) / 60) Mod 60
    
        'Hours'
        Hrs = ((Time - (Sec + (Min * 60))) / 3600) Mod 60
    
        Return Format(Hrs, "00") & ":" & Format(Min, "00") & ":" & Format(Sec, "00")
    End Function
    

    You pass the time (in seconds) you'd like to display on the label's text and the time will be formatted as you like it.

    e.g.

    lblTime.Text = GetTime(90)
    

    This will display 00:01:30 on the label.


    For reference, you can see this project I submitted on FreeVBCode some time ago. The only caveat is the project is in VB6. You should be able to open it in Visual Studio though.

提交回复
热议问题