VB2019 VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Codes 中文VB About Us

Digital Stopwatch



We can design a stopwatch that resembles a typical digital stopwatch.In this program, you need to insert one label, three command buttons and a timer. The label is used as the display panel. The buttons are use for Start, Stop amd Reset purpose. We need to write suitable code for each of these buttons. Set the timer1 interval to 100 which is equivalent to a tenth of a second.Besides that, declare three variables t, s and m to cater for a tenth of a second, second and minute display. We have also created a subroutine known as displaytime()to diplay the time.

The code for the subroutine displaytime() is:

LblDisplay.Caption = Format(m, "00") + ":" + Format(s, "00") + ":" + Format(t, "00")

The Format function is to ensure the time display is in the form of 00:00:00

The code for the timer1 is:

t = t + 1
 displaytime

   If t > 10 Then
            t = 0
            If s < 60 Then
                s = s + 1
            Else
                s = 0
                m = m + 1
            End If

        End If

The code

Dim t, s, m As Integer

Sub displaytime()
LblDisplay.Caption = Format(m, "00") + ":" + Format(s, "00") + ":" + Format(t, "00")
End Sub

Private Sub CmdReset_Click()
LblDisplay.Caption = "00" + ":" + "00" + ":" + "00"
        t = 0
        s = 0
        m = 0
End Sub

Private Sub CmdStart_Click()
t = 0
s = 0
m = 0
Timer1.Enabled = True
End Sub

Private Sub CmdStop_Click()
 Timer1.Enabled = False
End Sub

Private Sub Form_Load()
LblDisplay.Caption = "00" + ":" + "00" + ":" + "00"
End Sub

Private Sub Timer1_Timer()
t = t + 1
 displaytime

   If t > 10 Then
            t = 0
            If s < 60 Then
                s = s + 1
            Else
                s = 0
                m = m + 1
            End If

        End If         

End Sub

The Runtime Interface



Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy