What's new

Visual Basic Programming (VB5): Countdown??? (1 Viewer)

Mark Shannon

Screenwriter
Joined
May 27, 2002
Messages
1,991
Ok, I've taken a class in visual basic programming... I know the basics, ie: how to make a system clock, buttons, timers, etc...

what I've been toying with is making a countdown clock...

I want to make a program that can count down to a predetermined date (like someone's birthday, a movie release)

any of you expert (heh) programmers out there can help me out?

If possible, point me to a website that would teach me how to do this, or (if you have the time), make a sample code for me, and I can fill in the particulars...

Thanks ahead...
 

Kevin P

Screenwriter
Joined
Jan 18, 1999
Messages
1,439
How do you want to count down? By seconds, minutes, hours, days, weeks? I wrote one years ago (I used it to count down to my wedding!) but I don't think I have it anymore.

Hint: look up the Dateadd and Datediff functions in the online help. These should get you started.

KJP
 

Mark Shannon

Screenwriter
Joined
May 27, 2002
Messages
1,991
Well I was planning on having it countdown similar to:

(ex) 5 Days, 15 Hours, 56 Minutes, 13 Seconds Until ________

is it possible to do that...

I would use a label and a simple timer at a one second interval, right?

thanx anywayz, maybe those terms will help get me started...
 

Steven K

Supporting Actor
Joined
Jan 10, 2000
Messages
830
Sorry, dont know VB that well but if you are looking for a CC++ example, let me know.

What you'll want to do is to set up your application (assuming that it is single-threaded) so that it puts the main thread to sleep for XXX milliseconds in a loop, where XXX is the number of milliseconds of precision that you want your clock to maintain.
 

Kevin P

Screenwriter
Joined
Jan 18, 1999
Messages
1,439
Actually, you don't need a loop. Just use the Timer control to trigger an event once per second, and update the display on that event. To calculate the days, hours, minutes, seconds, etc., do the following:
Code:
dim l as long
 dim targetdate as date
 targetdate = #12/25/2003 15:00:00# ' count down to 3 PM Christmas
 l = datediff("s", now, targetdate)
This will give you the number of seconds from now (current date/time) until targetdate. Then you can figure out days, hours, and minutes by dividing the seconds by 86400, 3600, and 60 respectively.
 

Mark Shannon

Screenwriter
Joined
May 27, 2002
Messages
1,991
Ok, this is what I've come up with...paste it into VB if you want and try it out...

Private Sub tmrCountdown_Timer()
Dim lngL As Long
Dim targetdate As Date
Dim sglDays As Single
Dim sglHours As Single
Dim sglMinutes As Single
targetdate = #6/9/2003# 'countdown to my birthday
lngL = DateDiff("s", Now, targetdate)
sglDays = lngL / 86400
sglHours = 86400 / sglDays / 3600
sglMinutes = intHours / 60
lblCountdown.Caption = "There are " & sglDays & " days, " & sglHours & " hours, " & sglMinutes & " minutes, and " & lngL & " seconds until my birthday."
End Sub


Can someone tell me where I'm going wrong...I'm trying to have it so it will display the remaining units, not the total of each unit...it's late and I can't really think right now...I probably should have used integer instead of single, right?
 

Phil Kim

Stunt Coordinator
Joined
Aug 31, 1998
Messages
206
Just FYI, if you are thinking of pursuing a career in VB, I strongly recommend taking a refresher in VB.NET. VB has changed quite dramatically in VB.NET (very Java-like).
 

Kevin P

Screenwriter
Joined
Jan 18, 1999
Messages
1,439
Try this instead:
Code:
Private Sub tmrCountdown_Timer()
 Dim lngL As Long
 Dim targetdate As Date
 Dim lngDays As Long
 Dim lngHours As Long
 Dim lngMinutes As Long
 Dim lngSeconds As Long
 
 targetdate = #6/9/2003# 'countdown to my birthday
 lngL = DateDiff("s", Now, targetdate)
 lngSeconds = lngL Mod 60
 lngL = lngL 60
 lngMinutes = lngL Mod 60
 lngL = lngL 60
 lngHours = lngL Mod 24
 lngDays = lngL 24
 lblCountdown.Caption = "There are " & lngDays & " days, " & _
 lngHours & " hours, " & lngMinutes & " minutes, and " & _
 lngSeconds & " seconds until my birthday."
 End Sub


The Mod operator divides and returns the remainder, so it's used to extract out the seconds, minutes, and hours respectively, then the operator divides and truncates rather than rounding to the nearest integer (so 3 2 = 1, whereas 3 / 2 = 2 if using integer variables).

I used Longs rather than Singles as well since there's no need to use fractions (decimals) in this type of application.

KJP
 

Mark Shannon

Screenwriter
Joined
May 27, 2002
Messages
1,991
THAT'S IT!!!

I knew I should have used the Mod function....

I took a course in it in school last semester, but my other studies seemed to have pushed all of that stuff out of my head..

I learned Mod too!..just forgot how to use it...
 

Users who are viewing this thread

Sign up for our newsletter

and receive essential news, curated deals, and much more







You will only receive emails from us. We will never sell or distribute your email address to third party companies at any time.

Forum statistics

Threads
357,016
Messages
5,128,479
Members
144,241
Latest member
acinstallation449
Recent bookmarks
0
Top