Solved

Timer Control - Countdown

Posted on 2006-11-07
4
223 Views
Last Modified: 2010-04-23
Hi

I would like to close a form after 5 minutes, on the form load event I have:

Timer1.Interval = 300000
Timer1.Enabled = True

and the tick:

Timer1.Enabled = False
Me.Close()

How do I populate a label with a real time countdown of the time left before the form closes?

Thanks
0
Comment
Question by:weight01
  • 3
4 Comments
 
LVL 13

Accepted Solution

by:
newyuppie earned 500 total points
ID: 17894785
if you are using VB2005 you could take advantage of the new Stopwatch class:

imports system.diagnostics
dim sw as new stopwatch
sw.start


also you would need to set a new timer with the interval set to 1 second

dim Timer2 as new Timer
timer2.interval = 1000

and in the tick for this timer:
dim sec as integer = sw.elapsedmilliseconds * 1000
label1.text = sec.tostring()
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17894838
i forgot you needed a countdown, the code above will do a "countup". anyway, you only need to modify the code in the tick event of timer2 to a substraction like:

dim sec as integer = 5 - sw.elapsedmilliseconds * 1000
label1.text = sec.tostring()


on the other hand, if you dont have VB2005 you could use a more rudimentary approach, such as establishing a variable when your form loads that stores the value of the date and time the form opened, and then using the timer's tick to read the substraction from NOW and that variable you have set up before.

in the Load event:

Dim _dtStarted as DateTime = DateTime.Now

using the tick event of the TImer2 i commented before:
  dim tsElapsed as TimeSpan = 5 - DateTime.Now.Subtract(_dtStarted)
  label1.text = tsElapsed.TotalSeconds.ToString()



0
 
LVL 1

Author Comment

by:weight01
ID: 17897038
Many Thanks

Your help is appreciated
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17897883
thanks for the grade.
NY
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now