Timer: Label format

Fritz

Freshman
Joined
Oct 6, 2002
Messages
40
Location
switzerland
I have a timer to count in seconds:

Private counter As Integer
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
counter += 1
lblTimer.Text = counter.ToString.Format(TimeString)
End Sub

So far thats ok, but I would like it to start by 00:00.00. How can I do this?
 
I'm not sure I understand what you mean, but if you swap the
position of increasing counter and setting the label's Text (increase
the counter last in the sub), then in the first timer tick counter will
be 0.

What is TimeString?
 
I'm sure there's a better and shorter solution.
This is really messy and long but it works.
Visual Basic:
'Declare these at the top of your page (inside the Class)
    Private intCounter As Integer = 0
    Private intSeconds As Integer = 0
    Private intMinutes As Integer = 0
    Private intHours As Integer = 0

'here goes....

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Me.Text = FormatCounter()

    End Sub

    Private Function FormatCounter() As String
        Dim sTemp As String

        If intCounter < 60 Then
            intSeconds = intCounter
        Else
            intMinutes += 1
            intCounter = 0
            intSeconds = 0
        End If

        If intMinutes >= 60 Then
            intSeconds = 0
            intCounter = 0
            intMinutes = 0
            intHours += 1
        End If

        intCounter += 1

        If intHours < 10 Then
            sTemp = "0" & intHours.ToString & ":"
        Else
            sTemp = intHours.ToString
        End If
        If intMinutes < 10 Then
            sTemp = sTemp & "0" & intMinutes.ToString & ":"
        Else
            sTemp = sTemp & intMinutes.ToString
        End If
        If intSeconds < 10 Then
            sTemp = sTemp & "0" & intSeconds.ToString
        Else
            sTemp = sTemp & intSeconds.ToString
        End If

        Return sTemp

    End Function
 
Back
Top