Stop Watch in VB2008

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
1 post Page 1 of 1
Contributors
User avatar
bjm0008
Dedicated Member
Dedicated Member
Posts: 69
Joined: Thu Aug 13, 2009 2:35 am

Stop Watch in VB2008
bjm0008
Only 1 form:
=Form1=
- 1 label ("0 : 0 : 0 : 0")
- another label ("Topmost?")
- 5 buttons (Start, Pause, Resume, Clear, Exit)
- 1 Timer (EXTREMELY IMPORTANT!)

Image

Button1 - Start
Button2 - Pause
Button3 - Resume
Button4 - Clear (or reset)
Button5 - Exit

(The resume button should be behind the pause button to do that go to Format>Order>Send to back)
Image

=Code=
(ms = milliseconds)
(s = seconds)
(m = minutes)
(h = hours)
Code: Select all

Public Class Form1
    Dim ms As Integer = 0
    Dim s As Integer = 0
    Dim m As Integer = 0
    Dim h As Integer = 0
    Dim i As Boolean = False

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'For every time the timer counts 1 milisecond 'ms' goes up by 10
        ms = ms + 1 * 10
        'If the milliseconds go to 1000 then 'ms' goes to 0 and the seconds go up by 1 (For every 1000 millisecond 1 second goes by)
        If ms = 1000 Then
            ms = 0
            s = s + 1
        End If
        'When the seconds go to 60 (seconds in a minute) then 's' goes to 0 and the minutes (m) go up by 1
        If s = 60 Then
            s = 0
            m = m + 1
        End If
        'When the minutes reach 60 (minutes in an hour) then 'm' goes to 0 and the hours (h) go up by 1
        If m = 60 Then
            m = 0
            h = h + 1
        End If
        'Label1's Text is ('hours' : 'minutes' : 'seconds' : 'milliseconds')
        Label1.Text = (h & " : " & m & " : " & s & " : " & ms)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Enabled = False
        Button2.Visible = False
        Button2.Enabled = False
        Button3.Enabled = True
        Button3.Visible = True
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Label1.Text = ("0 : 0 : 0 : 0")
        Timer1.Enabled = False
        Button1.Enabled = True
        Button2.Enabled = False
        Button3.Enabled = False
        Button3.Visible = False
        Button2.Visible = True
        s = 0
        m = 0
        h = 0
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Timer1.Enabled = True
        Button3.Enabled = False
        Button3.Visible = False
        Button2.Enabled = True
        Button2.Visible = True
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 1
        Timer2.Enabled = True
        Button2.Enabled = False
        Button3.Enabled = False
        Button3.Visible = False
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.Close()
    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
        If i = True Then
            i = False
        Else
            i = True
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If i = True Then
            Label2.Text = ("f")
        End If
        If i = False Then
            Label2.Text = ("t")
        End If
        If i = True Then
            Me.TopMost = True
        End If
        If i = False Then
            Me.TopMost = False
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Enabled = True
        Button1.Enabled = False
        Button2.Enabled = True
    End Sub
End Class

- There is an explination inside the code of what happens -


Where I put label2.text = ("F") -
and Label2.text = ("T")
You can change the ("T") to ("Topmost?")
And the ("F") to ("Not topmost") -- or something like that...




-----If anybody can help me with the millisecond part (so it counts by ones instead of tens) that would be helpful.
Image
1 post Page 1 of 1
Return to “Tutorials”