Solved

Timer Control - Countdown

Posted on 2006-11-07
4
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

696 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