Timer Controls - Programmers Heaven

Howdy, Stranger!

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


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Timer Controls

shankar_nsmshankar_nsm Posts: 24Member
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, USAPosts: 684Member ✭✭✭
    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.