Solved

Timer Control - Countdown

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call windows 10 virtual keyboard from windows forms app 2 50
Complex SQL statement in VB.NET 7 31
FInd Image Control Gridview 3 19
Open a link in vb.net 2 15
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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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