Visual Timer

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
-How do I create a visible Timer? One that the user can see counting down? Also, how do I get a timer to reset once a button is pressed?
K, Does anyone know how to make a timer that the user can see? I've been told that it involves a label somehow, but that's the extent of my knowledge.
 
Last edited by a moderator:
Can you be more specific? A timer is just a component that runs a method every N milliseconds. What is there to look at?
 
juggernot, search the forums here for "countdown", it has been answered several times. Also post new topic questions in new threads, don't just append new unrelated questions to any of your existing posts. "One topic each thread, on thread each topic".
 
I've been searching, but can't find exactly what I need. And to be more specific, I'd like a label to show how much time the user has left. So basically, just a timer that shows up in a label and updates every second.
 
I've made a slight breakthrough, and can now display the time in a label, but I have no idea how to get it to refresh every second.
 
You'll need to implement the refreshing yourself, and you will also need a timer component, set it's interval to 1000 which will be about a second.Then in the tick event change the display in the label and call

VB.NET:
Label.Invalidate.

It will be up to your own logic to determin how the time get's updated but i would think that a separate variable for Hours, Minutes and Seconds would be the way to go. In the tick event increase or decrease the second variable by 1

VB.NET:
SecondVariable +=1
Label.Invalidate

It will then be up to some further logic to increase/decrease the minute variable when the second variable hits 60 and reset the second variable to 0
 
this is coming from a noob, but why doesn't this work:

Do Until Val(Me.Timer1.Interval) = 0
Me.TimeLabel.Text = Val(Me.Timer1.Interval) / 1000
If Val(Me.Timer1.Interval) = 0 Then Exit Do

I've tried it, and it freezes the program up, but in my head it seems like a simple solution.
 
I was googling for an answer, and I found this:

Another solution, is to have the timer execute every 1 second (1000), and then keep an integer variable. Set The integer variable to 20 right off the bat, and every time the timer fires it's event (every second) decrement the variable: variable = variable - 1
Then do an if statement, and test the value of the variable. If it's 0, 20 seconds have passed. You could do this with a label, also, and set the caption of the label to the value of the variable in the timer event. This would show the user how much time is left, and keep your program in count.

while they were talking about a different example, it sounds like it could work. What would the code look like though?

Also, would disabling the timer on button press, and then enabling it on another button press restart it?
 
k, what about this. Creating a second timer that is one second long, than getting it to do this:
TimeLabel1.text = VAl(me.Timer1.Interval)

The only problem is it seems to only do it once...
 
No, as i said create three variables

VB.NET:
Second as integer
Minute as integer
Hour as integer

The label text would then look something like..

Label.text = Hour.tostring + " : " Minute.tostring + " : " + Second.tostring

then in the tick event of the timer increment the second variable

VB.NET:
Private Sub MyTimer_Tick(blah blah) Handles MyTimer.Tick
Second +=1
Label.Invalidate
End Sub

You will then need some further logic to handle what happens when the seconds hit 60 i.e increment the minute variable and reset the second variable to 0

With Me?
 
Thanks for being so patient with my ignorance, I'm self teaching myself. Anyhow, I still need to find a way to make the timer repeat itself. Apparently the timer only runs once.
 
The timer will run until you stop it. Just use ..

VB.NET:
Timer.Enabled = true

VB.NET:
Private Sub MyTimer_Tick(blah blah) Handles MyTimer.Tick
Label.text = Hour.tostring + " : " Minute.tostring + " : " + Second.tostring

Second +=1
Label.Invalidate
End Sub

Add the code into the tick event as shown above. And it will work fine.
 
it sounds like it could work

It also sounds exactly like what vis781 said.


On vis' advice, I would be tempted to use a TimeSpan object instead of separate hours/min/seconds integers and associated rolling logic. TimeSpan can be modified after it is made (the seconds can be counted down) and it can be formatted to provide an hh:mm:ss notation
 
I did it kind of differently. I used 2 different timers. This is because I needed one to -1 from the Time variable every second, and I needed another to automatically run the check answer code after 60 seconds. I realize I could just have made better use of the variable, but this works to.

Then i used this code : Private Sub TimeLabel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TimeLabel.Paint
TimeLabel.Text = Second
End Sub

That worked out for me because of the invalidate code used earlier. Thanks for all your help guys.
 
Back
Top