Timer Controls

I am creating a application which should be run for a specific time period set by the user after which it should be the application should be closed and i need to display the time left before the application closes.
how to do this ?
I am new to programming


  • seancampbellseancampbell Pennsylvania, USA
    There are a few different ways to do this. I'd say the easiest is to use a Timer.

    1. Draw a Timer onto your main form.
    2. Draw a Label onto your main form.
    3. Declare a "Form Level" Date variable on the main form (Right under where it says 'windows designer generated code' and not inside of a subroutine) Dim startTime as Date and Dim endTime as Date.

    If the user says how many minutes it will be until it closes do this:

    Dim curTime as Date
    Dim endTime as Date

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    curTime = Now
    endTime = curTime.addMinutes(30)
    Timer1.Interval = 1000
    Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    curTime = Now
    Dim tSpan As TimeSpan = endTime.Subtract(curTime)
    Dim secondsLeft As Integer = tSpan.Seconds
    Dim minutesLeft As Integer = tSpan.Minutes
    If minutesLeft < 1 and SecondsLeft < 1 Then
    End If

    Label1.Text = Format(minutesLeft, "0") & ":" & Format(secondsLeft, "00")
    End Sub

    This should work for you
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion