PDA

View Full Version : Resetting a timer in VB6



Bruce
- 21st July 2008, 22:15
Is there some way to reset a timer in VB6?

What I need to do is reset a timer to zero each time a button is clicked. When the timer
interval is reached, it clears a text box, but I need to reset the timer if another button is
clicked before the timer interval is reached. Is this do-able?

skimask
- 21st July 2008, 22:24
Is there some way to reset a timer in VB6?
If you're talking about the TIMER control, wouldn't disabling and then re-enabling it reset it to zero?

Bruce
- 21st July 2008, 22:27
Nope. It just disables the timer event. It will fire at odd times once it's re-enabled because
it's not being reset to zero.

skimask
- 21st July 2008, 22:35
Nope. It just disables the timer event. It will fire at odd times once it's re-enabled because it's not being reset to zero.

Timer - as in timer that counts from 0 to 86400 during a day?
Or..
Timer - as in timer that will fire off an event every X milliseconds?
If it's the 1st one, I thought that one was driven by the 8259(or whatever it's equivalent is these days), a BIOS sort of thing that can't be reset with doing a 2 fingered salute (CMOS jumper reset :) )

Bruce
- 21st July 2008, 23:21
I have a VB form with multiple buttons. When a user presses a button, it prints the button
value to a text box. Button values range from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

The text box needs to be cleared after a timeout period of around 15 seconds, but only if
no other buttons are pressed during this time interval.

I.E. the user can enter 1, and it clears in the text box after 15 seconds. But if the user
enters 1, 2, 3, then the timer interval should be reset to zero after button #3 is pressed
so 123 in the text box is cleared in 15 seconds.

rhino
- 22nd July 2008, 02:57
Bruce -
Disable the timer, then set the interval property back to whatever you need.

Jerson
- 22nd July 2008, 03:08
I feel, it can't be done with the timer control as it is. Rewriting the interval does not reset the internal counting of the timer. I could suggest you look at this construct if you could use it.




sub Wait(interval as single)
Dim x as single

x = timer+interval
while x > timer: doevents:wend
end sub


This uses the inbuilt function 'Timer' to wait while the interval has not expired. This function is useful as an in-line timer.

Hope this helps.

Bruce
- 22nd July 2008, 03:56
Thanks guys, but neither of these approaches will work. Stopping & resetting the interval is
a no go, and a sub or proceedure that blocks button clicks isn't an option.

HenrikOlsson
- 22nd July 2008, 07:45
Hi Bruce,
Try disabling the Timer then set the interval to 0 and then set it back to whatever interval you want before enabling it again.

From this page (http://msdn.microsoft.com/en-us/library/aa235125(VS.60).aspx) on MSDN:

The Timer control's Enabled property determines whether the control responds to the passage of time. Set Enabled to False to turn a Timer control off, and to True to turn it on. When a Timer control is enabled, its countdown always starts from the value of its Interval property setting

So if you first set the interval to 0 and then reset it to whatever it should do what you want, right?

/Henrik.

rhino
- 22nd July 2008, 16:31
Hi Bruce,
Try disabling the Timer then set the interval to 0 and then set it back to whatever interval you want before enabling it again.

From this page (http://msdn.microsoft.com/en-us/library/aa235125(VS.60).aspx) on MSDN:


So if you first set the interval to 0 and then reset it to whatever it should do what you want, right?

/Henrik.

This method seems to work for me. Here's a quick test I used to verify. Set timer1 interval to 3000

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

Private Sub Command2_Click()
Label1.Caption = ""
End Sub

Private Sub Command3_Click()
Timer1.Enabled = False
Timer1.Interval = 0
Timer1.Interval = 3000
End Sub

Private Sub Timer1_Timer()
Label1.Caption = "DONE"
End Sub


Good call Henrik!

Bruce
- 23rd July 2008, 11:08
Woohoo...;o}

Thanks guys. That did the trick.

oparah
- 26th December 2013, 20:02
Private Sub Command1_Click() Timer1.Enabled = True End Sub Private Sub Command2_Click() Label1.Caption = "" End Sub Private Sub Command3_Click() Timer1.Enabled = False Timer1.Interval = 0 Timer1.Interval = 3000 End Sub Private Sub Timer1_Timer() Label1.Caption = "DONE" End Sub
Good call Henrik!




please pals were do i insert this code. is it the timer or the button use in starting the timre or the progress bar please help:frown: