Solved

Timer Control - Countdown

Posted on 2006-11-07
4
228 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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