Making a countdown

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Does anyone know how to make a countdown in vb.net?

I would like to use a textbox to enter a number in minutes. Once this is done the user clicks start, after the time has counted down a program will be run. In this case that program will be the shutdown program.

Thanks
Jon
 
use the timer control, when the user clicks the button have it set the timer interval based on the entered data (check to make sure it's numeric first, or only allow numbers in the textbox [read the keypress thread ta get that function i posted])

private const int60secs as integer = 60000 '1000 miliseconds in 1 second, 60 seconds in 1 minute 60*1,000 = 60,000 fyi

Timer1.Interval = convert.toint32(txtminutes.text) * int60secs
Timer1.Enabled = true

then in the timer's tick event run the shutdown process (or whatever program yer gonna run) there's a processes thread in here that'll show ya how to run a process/program
 
Right this is what I have so far

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2] timer [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Timers.Timer(1000)
 
[/size][size=2][color=#008000]'Private timer As New System.Timers.Timer(Me.textboxmins.text)
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ButtonStart_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] ButtonStart.Click
 
[/size][size=2][color=#0000ff]AddHandler[/color][/size][size=2] timer.Elapsed, [/size][size=2][color=#0000ff]AddressOf[/color][/size][size=2] TimerFired
 
Timer.Enabled = [/size][size=2][color=#0000ff]True
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] TimerFired([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Timers.ElapsedEventArgs)
 
Label2.Text = "Signal Time = " & e.SignalTime.ToString
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff]
[/color][/size]

The problem is that I can't seem to use the textboxmins value in the timer, I get this message instead


An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe
Additional information: Object reference not set to an instance of an object.

This is the line it occurs on:-


Private timer As New System.Timers.Timer(Me.textboxmins.text)
 
try this:
VB.NET:
private intTime as integer = cint(textboxmins.text)

Private timer As New System.Timers.Timer(intTime)
 
Place the timer declaration in ButtonStart's Click event handler. No need to have the timer as a class member. Or, at least set the interval in the Click event handler.

If your textBox text property is non-numeric (like TextBox1) or is empty when the form loads, you're trying to instantiate the timer without a valid parameter (interval As Double).
 
ultimatly my suggestion would be to have a form with a button, textbox & timer (component tray) when the user clicks the button:
VB.NET:
if isnumeric(textbox.text)=true then
'set timer1 interval
'activate timer1
'....
end if
'....
end sub
 
I would also suggest using the timer in the System.Windows.Forms namespace (I think that is what JuggaloBrotha is refering to by the component tray remark). It's the simplest of the three available timers, although the timer in the System.Timers namespace is the most accurate.

Here is a link to an article describing the three different timers: Execute Tasks Periodically. The sample code is in C#, but the ideas presented are the same for VB.NET.
 
Back
Top