CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2012
    Posts
    2

    A timer in Hrs, Mins + Secs

    This is what I have so far.

    Code:
        Private Sub Label1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Label1.Text = Label1.Text + 0.01
        End Sub
    
        Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
    
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            If Label1.Text = 60 Then Label2.Text = Label2.Text + 1 And Label1.Text = 0
        End Sub
    
        Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
    
        End Sub
    
        Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
            If Label2.Text = 60 Then Label3.Text = Label3.Text + 1 And Label2.Text = 0
        End Sub
    End Class
    The first part (Label 1 + Timer 1) works fine and it counts seconds but I want it to count minutes and hours too. I am quite new to VB so excuse any obvious mistakes. Help is much appreciated

    -Skrill

  2. #2
    Join Date
    May 2012
    Posts
    2

    Re: A timer in Hrs, Mins + Secs

    Got it working.
    Here's the code I replaced the above with.
    Code:
        Private Sec As Integer = 0, Min As Integer = 0, Hours As Integer = 0
        Private Sub Timer1_Tick() Handles Timer1.Tick
            Sec += 1
            If Sec = 60 Then Min += 1 : Sec = 0
            If Min = 60 Then Hours += 1 : Min = 0
            Label1.Text = Hours.ToString() & ":" & Min.ToString() & ":" & Sec.ToString()
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured