Countdown

  • Thread starter Thread starter Yet Another One
  • Start date Start date
Y

Yet Another One

Sorry if this has been answered before, but I simply cannot find the
solution on my own.

I am looking to do a countdown in my application. Basically, I need to
count down from 5:59 (5 minutes, 59 seconds) and then perform a task.
Performing the task is easy enough, but the countdown is just slipping
past me.

Anyone have any idea how I can best achieve this?

Thanks in advance and sorry for wasting your time. I am quite new to
this and trying to learn as much as I can on my own.


Ryan
 
try using a timer control. this has a tick event, so you can set the timer to
go off every second and countdown the time. the interval is in milliseconds,
so 1000 = 1 second. all you need to do is keep track of the seconds and
minutes and when you hit your goal, reset the minutes and seconds, stop the
timer, do what you need to, then start the timer. hope this helps!
 
Hi,

To be more precise in the answer from Iwdu15

Use the forms timer and don't forget to reset it everytime inside its event
otherwise you can get the craziest results.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.timer(VS.80).aspx

I hope this helps,

Cor

Thanks for the replies.

I understand that I have to use a timer to do this (I should have been
more clear, sorry), but my problem is that I don't know HOW to count
down minutes like that.

For instance; How do I subtract 1 from the minutes when the seconds
reach zero and then reset the seconds to 59? I simply cannot figure
this out, hehe. As I said, I am new to this and trying to learn on my
own. Probably a bad idea, yes, but reading the help has only gotten me
so far with counting down the way I need to.

This project isn't anything important. It's just me trying to learn
some stuff about visual basic is all.

Thanks again,

Ryan
 
Yet Another One said:
Thanks for the replies.

I understand that I have to use a timer to do this (I should have been
more clear, sorry), but my problem is that I don't know HOW to count
down minutes like that.

For instance; How do I subtract 1 from the minutes when the seconds
reach zero and then reset the seconds to 59? I simply cannot figure
this out, hehe. As I said, I am new to this and trying to learn on my
own. Probably a bad idea, yes, but reading the help has only gotten me
so far with counting down the way I need to.

This project isn't anything important. It's just me trying to learn
some stuff about visual basic is all.

Thanks again,

Ryan

One way to do it is to use a member variable in the class or form you are
working with. You would initially set this var to 359 (in seconds) and
subtract one every tic from the timer control event. When it reaches 0, the
5 minutes 59 seconds has elapsed and you can call your function that does
the operation you want. Once done, you'd probably want to reset the var
back to 359 and start the timer again...

Mythran
 
One way to do it is to use a member variable in the class or form you are
working with. You would initially set this var to 359 (in seconds) and
subtract one every tic from the timer control event. When it reaches 0, the
5 minutes 59 seconds has elapsed and you can call your function that does
the operation you want. Once done, you'd probably want to reset the var
back to 359 and start the timer again...

Mythran

Thanks for the ideas, I really appreciate it.

Here is what I have so far... please be gentle and don't laugh, I am
still new to this. :)

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

Seconds = Seconds - 1
If Seconds = 0 Then
Minutes = Minutes - 1
Seconds = 59
Else
If Minutes = 0 And Seconds <= 11 Then
Minutes = Minutes
Else
Minutes = 5
End If
End If

....

End Sub

This seems to count down the way I want but with one problem... when
it never gets to 0:00, it always goes to -5:59 and THEN back to 5:59.

Can anyone help me clean this up a little? Basically, I need it to do
this:

Label text would equal 5:59... 5:58... 5:57... etc, counting down to
0:00 and then resetting to 5:59 without showing negative numbers.

All the help so far has been appreciated very much. It's given me
ideas and they seem to be somewhat working, but not exactly as
desired.

Thanks!

Ryan
 
I'm not going to be able to write the code (unless you can wait a week) but
let me give you an alternative. First you probably do not want to set a
timer using it to count the clicks. You _do_ want to use a timer but rather
than count clicks you just use the timer to give you an opportunity to check
the clock.

Let's say you need to set the timer for 5 minutes from now. Set the "target
time" to the current time plus 5 minutes. When the click event occurs you
can display the differerence between the target time and the current time.
That difference gets smaller as the real time approaches the target time.
At one point your timer event fires and the difference will be zero or
possibly a fraction of a second negative. If you compute the Max() of the
difference and zero you will never go negative. And when the difference is
zero you run whatever process you wanted. To reset the the "timer" you
simply reset the target time to another point in the future.

The actual time is the thing that adjusts (it is adjusting automatically
because the computer has a clock) and all you need is a non-incrementing
"static" value representing the target time. There is no need to increment
or update anything on each event.

Keep in mind that you'll need date/time math (or use a value that takes the
date into consideration) in case it is (say) 3 minutes before midnight and
you set the target time 5 minutes ahead. If you don't notice the date
changed your time won't match until some 23 hours and 55 minutes later.

Does this make any sense?
Tom
 
I'm not going to be able to write the code (unless you can wait a week) but
let me give you an alternative. First you probably do not want to set a
timer using it to count the clicks. You _do_ want to use a timer but rather
than count clicks you just use the timer to give you an opportunity to check
the clock.

Let's say you need to set the timer for 5 minutes from now. Set the "target
time" to the current time plus 5 minutes. When the click event occurs you
can display the differerence between the target time and the current time.
That difference gets smaller as the real time approaches the target time.
At one point your timer event fires and the difference will be zero or
possibly a fraction of a second negative. If you compute the Max() of the
difference and zero you will never go negative. And when the difference is
zero you run whatever process you wanted. To reset the the "timer" you
simply reset the target time to another point in the future.

The actual time is the thing that adjusts (it is adjusting automatically
because the computer has a clock) and all you need is a non-incrementing
"static" value representing the target time. There is no need to increment
or update anything on each event.

Keep in mind that you'll need date/time math (or use a value that takes the
date into consideration) in case it is (say) 3 minutes before midnight and
you set the target time 5 minutes ahead. If you don't notice the date
changed your time won't match until some 23 hours and 55 minutes later.

Does this make any sense?
Tom


Hello Tom and thanks for your reply.

I get what you are saying, but I have no idea how to implement it. I
had a hard enough time coming up with the little bit of code I posted
earlier, hehe.

Thanks!

Ryan
 
Hi,

Maybe you could try approaching the problem from this angle.

Dim t As New DateTime()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Timer1.Enabled = True
t = t.AddMinutes(359)
Label1.Text = t.ToShortTimeString
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
t = t.AddMinutes(-1)
Label1.Text = t.ToShortTimeString
If t.Ticks = 0 Then
t = t.AddMinutes(359)
Label1.Text = t.ToShortTimeString
End If
End Sub

This isn't a complete solution depending on what you are after, because it
isn't guaranteed to be an exact timed countdown, but I hope it helps.

Kind regards,

Martin.
 
Hi,

Maybe you could try approaching the problem from this angle.

Dim t As New DateTime()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Timer1.Enabled = True
t = t.AddMinutes(359)
Label1.Text = t.ToShortTimeString
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
t = t.AddMinutes(-1)
Label1.Text = t.ToShortTimeString
If t.Ticks = 0 Then
t = t.AddMinutes(359)
Label1.Text = t.ToShortTimeString
End If
End Sub

This isn't a complete solution depending on what you are after, because it
isn't guaranteed to be an exact timed countdown, but I hope it helps.

Kind regards,

Martin.


Hello,

Thanks for your solution. It DOES count down the way I want it to, but
it also displays AM after the timer numbers. I will play with this
some more and see what I can do with it.

Thanks again!

Ryan
 
Back
Top