|
-
Dec 4th, 2000, 12:47 PM
#1
Thread Starter
New Member
Hy there people of the world.Please help me with ,What I thought was a going to be simple piece of code but I have become just a little STUCK!!!
I am trying to make just a simple "Stop Watch" program where you can start a timer then stop it,and start the timer from where it was stoped.
I have got the Stop Watch sort of working,it starts counting from 00:00:00,but when i stop the Stop Watch and restart it,it starts counting from the time a stoped it plus the time it was stoped for.
Any ideas on how to fix it
PS.I am new to Programing so i am a bit Stupid
thanks
-
Dec 4th, 2000, 12:52 PM
#2
Fanatic Member
Can you post your code here?
-
Dec 4th, 2000, 01:13 PM
#3
Fanatic Member
-
Dec 4th, 2000, 05:40 PM
#4
Addicted Member
I haven't really read your code but may be this may work?
Make the variable that tracks the time global so that you can initialise it when you click start. This way it's available to the timer sub procedure and the command event procedure.
-
Dec 4th, 2000, 05:49 PM
#5
Addicted Member
-
Dec 4th, 2000, 06:11 PM
#6
Addicted Member
After reading about your thread I thought I'll have a go at writting a stop watch as I have never done one before and thought It could be intresting, hers my code:
However it doesn't seem to be counting at the right pace if you know what I mean the seconds are to slow...any ideas?
Code:
Dim stopWatchHours As Integer
Dim stopWatchMinutes As Integer
Dim stopWatchSeconds As Integer
Dim stopWatchMicroSecs As Integer
Private Sub cmdStartStop_Click()
If cmdStartStop.Caption = "Start" Then
cmdStartStop.Caption = "Stop"
'initialise variables
stopWatchHours = 0
stopWatchMinutes = 0
stopWatchSeconds = 0
stopWatchMicroSecs = 0
'start the timer
Timer1.Enabled = True
Else
cmdStartStop.Caption = "Start"
'stop the timer
Timer1.Enabled = False
End If
End Sub
Private Sub Timer1_Timer()
stopWatchMicroSecs = stopWatchMicroSecs + 1
If stopWatchMicroSecs = 100 Then
stopWatchSeconds = stopWatchSeconds + 1
stopWatchMicroSecs = 0
End If
If stopWatchSeconds = 60 Then
stopWatchMinutes = stopWatchMinutes + 1
stopWatchSeconds = 0
End If
If stopWatchMinutes = 60 Then
stopWatchHours = stopWatchHours + 1
stopWatchMinutes = 0
End If
Label1.Caption = stopWatchHours & ":" & stopWatchMinutes & ":" & stopWatchSeconds & ":" & stopWatchMicroSecs
End Sub
-
Dec 5th, 2000, 03:36 AM
#7
Fanatic Member
-
Dec 5th, 2000, 06:03 AM
#8
PowerPoster
How abt mine?
Code:
'Put this code under a Form
Private Sub CmdAction_Click(Index As Integer)
Dim i As Integer
Select Case Index
Case 0 'Start
sTime = Now
lblStopWatch.Caption = "00:00:00"
SetTimer Me.hwnd, 0, 1000, AddressOf TimerProc
CmdAction(0).Enabled = False
For i = 1 To 3: CmdAction(i).Enabled = True: Next
Case 1 'Stop
KillTimer Me.hwnd, 0
CmdAction(0).Enabled = True
For i = 1 To 3: CmdAction(i).Enabled = False: Next
Case 2 'Reset
sTime = Now
lblStopWatch.Caption = "00:00:00"
Case 3 'Pause
If CmdAction(3).Caption = "Pause" Then
pTime = Now
KillTimer Me.hwnd, 0
CmdAction(3).Caption = "Resume"
Else
SetTimer Me.hwnd, 0, 1000, AddressOf TimerProc
sTime = sTime + (Now - pTime)
CmdAction(3).Caption = "Pause"
End If
End Select
End Sub
'Put this code under the basic Module fiile
Option Explicit
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public sTime As Date
Public eTime As Date
Public pTime As Date
Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
eTime = Now
Ctime
End Sub
Private Sub Ctime()
Dim h As Long
Dim m As Long
Dim s As Long
s = DateDiff("s", sTime, eTime, 0, 0)
h = s \ 3600
m = (s - h * 3600) \ 60
s = (s Mod 60)
frmMain.lblStopWatch.Caption = h & ":" & Format(m, "00") & ":" & Format(s, "00")
End Sub
-
Dec 5th, 2000, 04:55 PM
#9
Addicted Member
I initialised the variables in the button's event procedure because every time a user clicks the start button it should reset first.
I was trying to write it very basic without using the system time in any way or APIs. Plus I wrote it in like 2 minutes while replying to this thread. Any ideas why the seconds are not right?
[Edited by Ramandeep on 12-05-2000 at 05:11 PM]
-
Dec 5th, 2000, 08:29 PM
#10
PowerPoster
Hi! Ramandeep
I juz run your code & everything is fine. Perhaps you set a wrong interval for your timer? I've set the timer interval to 1.
-
Dec 7th, 2000, 02:18 PM
#11
Addicted Member
Chris I have set my interval to 1 but the seconds don't seem to be changing at the right speed, another thing I've noticed is my PinPong Game (another recent thing I wrote/still edditing it)is that when I run it at my UNI it runs faster, asny ideas why this may be?
It can be the computer spec because my machine out runs the uni's tosters by mile
My Spec
PIII 800MHz
Dual Proccessor Mother Boards (only installed 1 CPU at the moment)
256Mb Ram
Geforce 2 graphics card
Live Platinum 5.1 sound card
20.4Gb harddrive (7200rpm)
Win98 Second Eddition
Visual Basic 6.0 Enterprise Eddition
+more
I'm not trying to bost about my machine's spec!!!
Any ideas? Chris what sort off machine did u run it on and did the seconds change at the right speed? If so it could be that somthing isn't configured right?
-
Dec 7th, 2000, 08:22 PM
#12
PowerPoster
Hi! Ramandeep, I don't think this is due to the hardware configuration isuue. Well, my PC is Juz the Dell Dimension L550r [*]PIII 550MHz,[*]128MB Ram
Perhaps, you're running others application that take too much resource and cause your stopwatch running into inaccuratecy.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|