• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1958
  • Last Modified:

Vb6 : Timer And Seconds Counting

Id like to start a timer to count in seconds when i click a command button


what would the code be ??? vb6:

output to label or text box, or better.

thankyou
0
Jimmyx1000
Asked:
Jimmyx1000
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Create a new project and add a timer, label and button:

Option Explicit

Private startTime As Date

Private Sub Form_Load()
    Timer1.Interval = 333 ' update counter 3 times a second (adjust to your liking)
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    startTime = Now
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Label1.Caption = DateDiff("s", startTime, Now)
End Sub
0
 
gimmeadrinkCommented:
Im sure if you search around, you will find many ppl saying how inaccurate the timer is.

If dead accuracy is not that much of an issue for you, then the timer as suggested above is perfect.

If accuracy is important, then i highly reccomend ccrptimer. It was developed by  Karl E. Peterson who is one of microsofts Most Vaulable Professionals. I only using the library now, its fantastic.

The other great thing about it... its not a control, its a library, meaning you dont need one on each form u need a timer, you dont even need a form at all.

Its available here: http://ccrp.mvps.org/index.html?controls/ccrptimer6.htm 

Its very simple to use and has a sample project.

If you want any help using it, just let me know.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
InVisalCommented:
Dim a As Integer
Private Sub Form_Load()
    Timer1.Interval = 1000
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    a = 0
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    a = a + 1
    Label1.Caption = a
End Sub
0
 
Camden04Commented:
Sub DelayPre(preset)
 'delay in seconds.000

    Dim start#
    Dim Check#
    start = Timer
    Do Until Check >= start + preset
        DoEvents
        Check = Timer
    Loop
End Sub

Sub Command1_Click()
Dim x as Long
x = 0
   Do
      DelayPre(1)
      x = x + 1      
      Label1.Caption = x
   DoEvents
   Loop
End Sub
     
0
 
Jimmyx1000Author Commented:
InVisal

I need to put the

Doevents command

in your code where is
the correct place to put this in.

Dim a As Integer
Private Sub Form_Load()
    Timer1.Interval = 1000
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    a = 0
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    a = a + 1
    Label1.Caption = a
End Sub
0
 
InVisalCommented:
no need do events
it work fine
0
 
Jimmyx1000Author Commented:
how Can i get this function to display as h m s  : hours minutes seconds

thanks
0
 
InVisalCommented:
Dim s As Integer
Dim m As Integer
Dim h As Integer

Private Sub Form_Load()
    Timer1.Interval = 1000
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    s = 0
    m = 0
    h = 0
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    s = s + 1
   
    If s = 60 Then
        m = m + 1
        s = 0
    End If
   
    If m = 60 Then
        m = 0
        h = h + 1
    End If
   
    Label1.Caption = h & ":" & m & ":" & s
End Sub
0
 
InVisalCommented:
or this code

Dim a As Integer

Private Sub Form_Load()
    Timer1.Interval = 1000
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    a = 0
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
   
    Dim m, s, h As Integer

    a = a + 1
    m = Int(a / 60)
    s = a - (m * 60)
    h = Int(m / 60)
    m = m - (h * 60)
   
    Label1.Caption = h & ":" & m & ":" & s
   
End Sub
0
 
VBtormentCommented:
or this

dim StartTime


Private Sub Form_Load()
    Timer1.Interval = 500
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    StartTime = now
    Timer1.Enabled = True
End Sub


private sub Timer1_Timer()
    Label1.caption= Format$(Now - starttime, "HH:MM:SS")
End Sub

just reset StartTimer to now to start again
0
 
Jimmyx1000Author Commented:
InVisal

Your code display a result of

0:0:0 format

can you do a

00:00:00 format

apart from this that should be it

0
 
InVisalCommented:
yeah

Label1.Caption = Right("0" & h, 2) & ":" & Right("0" & m, 2) & ":" & Right("0" & s, 2)
0
 
Jimmyx1000Author Commented:
Ok thankyou expert

Well Done
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now