VB Timer

  • 13 years ago

    Hi All,

    I am working a class project that involves a VB stop watch, I am using start and stop command buttons, two text boxes for start time and one for running time, a text box to display elapsed time. Can anyone point me to some sample code to just retrieve the time in seconds from the timer control? I understand the setup of the timer for 1 second but am having problems retrieving the time in seconds.

    Thanks for any help.


  • 13 years ago

    I dont think you want to use a timer.

    A timer is not a clock - it is a way of generating an event at regular intervals.


    If you need to know seconds since something happened, just look at the  NOW function, which returns the current time.

    Example code follows: Create a form with a button and a textbox set to multi line.

    When the form loads, it stores the time.

    Every time you click the button, it will tell you the number of elapsed seconds since that point. You could use a timer to update a label with the current time...


    Dim t_now As Date
    Dim t_then As Date
    Dim elapsed_seconds As Double

    Private Sub Command1_Click()
      t_now = Now
      elapsed_seconds = (t_now - t_then) * 100000

      'show the time
      Text1.Text = Text1.Text & t_now & vbCrLf
      Text1.Text = Text1.Text & "Elapsed seconds: " & _

                Format(elapsed_seconds, "0.00") & vbCrLf

    End Sub

    Private Sub Form_Load()
        t_then = Now
       Text1.Text = ""
    End Sub




  • 13 years ago
    Thank you for your help. I really appreciate it.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“You can stand on the shoulders of giants OR a big enough pile of dwarfs, works either way.”