Timer countdown then launch program

jmasgalas

Member
Joined
Apr 21, 2007
Messages
15
Programming Experience
Beginner
I posted this to a different forum but now I think it belongs here...

First Hi all! I am new here. I am also just beginning to program using vb.net and Visual Studio 2005. I am having trouble creating a program that uses a timer.

I want a timer to start when the form loads at 3 minutes and countdown to 0. The timer should display the time left in a text box. When it reaches zero I want it to stop, launch an external program, and close the form.

Can anyone help me with this? I know it is simple but I am just learning.

Thanks so much!
 
Timer doesn't count down, it only triggers at the interval you set. Using the Date and TimeSpan data types you can calculate the time since start and subtract this from a 3 minutes timespan. The Process class can run the program.

First add the Timer from Toolbox Components to form, set its Interval to 1000 which is 1 second, its Enabled to True which mean it will be running from start. Here is all the code necessary:
VB.NET:
Private starttime As Date
 
Private Sub frm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    starttime = Date.Now
End Sub
 
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim countdown As New TimeSpan(0, 3, 0)
    countdown = countdown.Subtract(Date.Now.Subtract(starttime))
    CountdownTextBox.Text = String.Format("{0}:{1:00}", countdown.Minutes, countdown.Seconds)
    If countdown.TotalSeconds <= 0 Then
        Timer1.Enabled = False
        Process.Start("calc.exe")
    End If
End Sub
 
Thanks JohnH! I will try it when I get back to work on Monday. This is for a program I am trying to write at work for a group of users.
 
Hey John H that worked great. Would you mind explaining it a bit to me. I am having a hard time understanding the countdown variable declaration, how it works, and the text box formatting.

Basically I just wanted you to break it down for me so I can understand it better.

Thanks!
 
Back
Top