473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Countdown

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
May 3 '07 #1
9 2307
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!
--
-iwdu15
May 3 '07 #2
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/lib...er(VS.80).aspx

I hope this helps,

Cor
"Yet Another One" <me@here.comschreef in bericht
news:45********************************@4ax.com...
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

May 4 '07 #3
On Fri, 4 May 2007 03:18:06 +0200, "Cor Ligthert [MVP]"
<no************@planet.nlstaggered into the room, obviously drunk,
and said:
>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/lib...er(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
May 4 '07 #4


"Yet Another One" <me@here.comwrote in message
news:km********************************@4ax.com...
On Fri, 4 May 2007 03:18:06 +0200, "Cor Ligthert [MVP]"
<no************@planet.nlstaggered into the room, obviously drunk,
and said:
>>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/lib...er(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
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
May 4 '07 #5
On Fri, 4 May 2007 10:33:12 -0700, "Mythran" <ki********@hotmail.com>
staggered into the room, obviously drunk, and said:
>
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
May 5 '07 #6
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
"Yet Another One" <me@here.comwrote...
On Fri, 4 May 2007 10:33:12 -0700, "Mythran" <ki********@hotmail.com>
staggered into the room, obviously drunk, and said:
>>
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

May 5 '07 #7
On Fri, 4 May 2007 22:29:48 -0400, "Tom Leylan" <tl*****@nospam.net>
staggered into the room, obviously drunk, and said:
>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
May 5 '07 #8
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.
May 5 '07 #9
On Sat, 05 May 2007 12:23:08 GMT, "Martin" <@ntlworld.comstaggered
into the room, obviously drunk, and said:
>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
May 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Matt Stanley | last post by:
I am using the countdown script developed by Chris Nott on his website(http://www.dithered.com/javascript/countdown/example.html). The script counts down the days, hours, minutes and seconds to a...
3
by: Bonnett | last post by:
I have been creating a generic countdown timer (source code below), counting the seconds, minutes, hours and days till an event, but I have having trouble with it finding out how many hours are...
4
by: Christine | last post by:
I've implemented a countdown timer for a tutorial web page that should give the user 45 minutes to complete the test, only to find that the timer is slowly 'losing' time. On average, it actually...
8
by: Michael | last post by:
I have this script that works the way I want except for one thing... Once it hits zero it starts to count up and looks like this: -1:0-1:0-1:0-18 with the last number counting up. Can anyone...
31
by: Stephanie | last post by:
I have a newbie question (couldn't find the answer with google) How to show countdown from 10 to 0 seconds. Stephanie
4
by: henrik | last post by:
Hi. Can anyone tell me the python code for a simple countdown from eg. 2.00 minutes. It should be printet out to the screen. When it is finished it should write "Time is up" Hope you can...
4
by: tranky | last post by:
Hi programmers, i'm italian, and i need your help. Is it possible? There's a way to create a countdown control in asp.net? The control must shows in a label the countdown text. At the end of the...
0
by: Jorhajnes | last post by:
Hi there. Im very new to making games in flash, although I have used flash for quite some time now in web-based situations so I know my way around. Anyways, I thought it would be fun to try...
5
mageswar005
by: mageswar005 | last post by:
hi, I need a javascript countdown timer to my website, I have already the countdown timer but its have some problem. Note: 1) If i choose the tools/internet option in my browser means , at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.