473,545 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2315
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.comsch reef in bericht
news:45******** *************** *********@4ax.c om...
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.nlstagg ered 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.comwro te in message
news:km******** *************** *********@4ax.c om...
On Fri, 4 May 2007 03:18:06 +0200, "Cor Ligthert [MVP]"
<no************ @planet.nlstagg ered 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********@hot mail.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(ByV al sender As System.Object, ByVal e As
System.EventArg s) 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.comwro te...
On Fri, 4 May 2007 10:33:12 -0700, "Mythran" <ki********@hot mail.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(ByV al sender As System.Object, ByVal e As
System.EventArg s) 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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Me.Timer1.Enabl ed = True
t = t.AddMinutes(35 9)
Label1.Text = t.ToShortTimeSt ring
End Sub

Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As
System.EventArg s) Handles Timer1.Tick
t = t.AddMinutes(-1)
Label1.Text = t.ToShortTimeSt ring
If t.Ticks = 0 Then
t = t.AddMinutes(35 9)
Label1.Text = t.ToShortTimeSt ring
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.coms taggered
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(ByVa l sender As System.Object, ByVal e As
System.EventAr gs) Handles MyBase.Load
Me.Timer1.Enabl ed = True
t = t.AddMinutes(35 9)
Label1.Text = t.ToShortTimeSt ring
End Sub

Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As
System.EventAr gs) Handles Timer1.Tick
t = t.AddMinutes(-1)
Label1.Text = t.ToShortTimeSt ring
If t.Ticks = 0 Then
t = t.AddMinutes(35 9)
Label1.Text = t.ToShortTimeSt ring
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
3253
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 specified date. His site at the link above provides the source code .js file, as well as instructions for how to install and configure the script...
3
2675
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 left. When i get it to display a complete countdown (days hours mins seconds left) it is one hour short when counting dates that fall in the following...
4
4352
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 takes an extra 35 seconds, but has taken an extra 2.5 minutes in some cases. Any ideas what might be the cause? And more importantly - the fix? ...
8
3736
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 help me find a way to stop it at zero? Thank you, Mike
31
14135
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
3521
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 help.
4
1907
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 countdown, the page must redirect to another. I don't want to use javascript. Is it possible?
0
2359
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 making some sort of game and I got stuck… :P I managed to get a clock counting down from 30 to 0 and then collect the score but I get in trouble when i...
5
2530
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 that time countdown cannot be stop. 2) If i refresh the page means the countdown should comes from begining time. this is for Browser session...
0
7473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7408
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7661
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7433
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7763
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5340
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
712
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.