Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • *Experts*
Posted

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?

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • Moderators
Posted

I'm sure there's a better and shorter solution.

This is really messy and long but it works.

'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

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...