Solved

Vb6 : Timer And Seconds Counting

Posted on 2004-10-02
14
1,944 Views
Last Modified: 2012-06-21
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
Comment
Question by:Jimmyx1000
14 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12208133
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12208201
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
 
LVL 2

Expert Comment

by:gimmeadrink
ID: 12209894
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
 
LVL 1

Accepted Solution

by:
InVisal earned 500 total points
ID: 12209958
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
 

Expert Comment

by:Camden04
ID: 12209990
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
 

Author Comment

by:Jimmyx1000
ID: 12210544
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
 
LVL 1

Expert Comment

by:InVisal
ID: 12210816
no need do events
it work fine
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Jimmyx1000
ID: 12210884
how Can i get this function to display as h m s  : hours minutes seconds

thanks
0
 
LVL 1

Expert Comment

by:InVisal
ID: 12210942
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
 
LVL 1

Expert Comment

by:InVisal
ID: 12210966
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
 
LVL 1

Expert Comment

by:VBtorment
ID: 12211029
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
 

Author Comment

by:Jimmyx1000
ID: 12211077
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
 
LVL 1

Expert Comment

by:InVisal
ID: 12211100
yeah

Label1.Caption = Right("0" & h, 2) & ":" & Right("0" & m, 2) & ":" & Right("0" & s, 2)
0
 

Author Comment

by:Jimmyx1000
ID: 12211144
Ok thankyou expert

Well Done
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now