Creating a Timer

This time I want to show how to create a timer in visual basic. Net using the timer component.

Well, do not pretend that this is a step by step example showing each of the modified properties of each control, on the contrary, I want to concentrate on the functionality of the timer and I will let you design. Just show them that I did.

The controls used are: a form, three buttons and two labels. We also need to add a Timer to the form. It would look more or month as follows:

But the first thing we will define four private variables at the level of form.

 Private hours As Integer = 0
 Private minutes As Integer = 0
 Private second As Integer = 0
 Private milliseconds As Integer = 0

The Timer component must have its interval property to 100.

We will create a function to display in label1 over time:

     Sub printTime ()
         Label1.Text = hora.ToString.PadLeft (2, "0") & ":"
         Minuto.ToString.PadLeft Label1.Text & = (2, "0") & ":"
         Segundo.ToString.PadLeft Label1.Text & = (2, "0") & ":"
         Milisegundo.ToString.PadLeft Label1.Text & = (1, "0")

         Label1.Refresh ()
     End Sub

In the tick event of the Timer component is where you place all the logic of the stopwatch. Copy the following code there:

 + = 1 millisecond
 Then if millisecond = 9
     millisecond = 0
     second + = 1
     If seconds = 59 Then
         second = 0
         + = 1 minute
         Then If = 59 minutes
             minute = 0
             hour + = 1
         End If
     End If
 End If
 printTime ()

Well, what remains is the code of the three buttons. The start button activates the timer:

 Timer1.Enabled = True

The Pause button stops the timer:

 Timer1.Enabled = False

And the reset button to reset the values ​​of the variables:

 time = 0
 minute = 0
 second = 0
 millisecond = 0

 printTime ()

I hope that this example does not prove complicated and above all that will be useful. Finally I leave you a snapshot of the stopwatch is running:

32 Responses to "How to Create a Stopwatch"

  1. Information Bitacoras.com ...

    Rate in Bitacoras.com: This time I want to show how to create a timer in visual basic. Net using the timer component. Well, do not pretend that this is a step by step example showing each of the modified properties of ...

  2. Edith Bautista says:

    Thank you very much I was just beginning with very useful. Net and well described.

    Goodbye.

  3. mezaq junior says:

    Examples are very good ... and thanks for contributing knowledge for q and q tiens recently started ...

  4. Jan says:

    Hello!
    px thank you very much was a tremendous help your code is more served me save my life lol ...
    not quite work thanks ...
    bye bye kuidate ...

  5. Adyir says:

    Thank you very much man ... without complications this example, very very good, that God will help you as ...

  6. bacotich says:

    Very good contribution is so I congratulate you!

  7. galvis says:

    thank you very much helped me a lot

  8. Fernando says:

    The timer is fine just not to the time after 59 seconds 00:01:59:0 not mind jumps from 58 to 00 and 59 does not exist, please check your own code

  9. Fernando says:

    The solution would be so
    + = 1 millisecond
    If Millisecond = 10 Then
    Millisecond = 0
    Second + = 1
    If second = 60 Then
    Second = 0
    Minute + = 1
    If Minute = 60 Then
    Min = 0
    Time + = 1
    End If
    End If
    End If
    PrintTime ()
    Thus the second or minute 59 would exist but are grateful for the contribution.

  10. Elflaco says:

    Very good, thank you very much!! I got my I'm duranga

  11. Darwin says:

    THANKS! I woke up the example logic xq make a mega project I hope to continue evaluating this example instance of this magnitude suviendo God bless

  12. Darwin says:

    I am new to programming haci do my project.

    Public Class Form1
    Private hours As Integer = 0
    Private minutes As Integer = 0
    Private second As Integer = 0
    Private milliseconds As Integer = 0
    Sub printTime ()

    Label1.Text = hora.ToString.PadLeft (2, "0") & ":"

    Minuto.ToString.PadLeft Label1.Text & = (2, "0") & ":"

    Segundo.ToString.PadLeft Label1.Text & = (2, "0") & ":"

    Milisegundo.ToString.PadLeft Label1.Text & = (1, "0")

    Label1.Refresh ()
    End Sub
    Timer1_Tick Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    + = 1 millisecond
    Then if millisecond = 9

    millisecond = 0
    second + = 1

    If seconds = 59 Then

    second = 0

    + = 1 minute

    Then If = 59 minutes

    minute = 0

    hour + = 1

    End If

    End If
    End If
    printTime ()
    End Sub

    Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Timer1.Enabled = True
    End Sub

    Private Sub Button2_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Timer1.Enabled = False
    End Sub

    Button4_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    End
    End Sub

  13. Jaime says:

    Thank you very much for the contribution

  14. CARRARO says:

    Good code!

  15. JonPrince says:

    Good code .... I served to perfection above corrected the error .... thanks

  16. CRIS says:

    thanks for your contribution served me very helpful

  17. Ripper says:

    Hello friend quisioera know where you make your blog??? Is this page?
    Can you help me : D

  18. romina says:

    I run this great wonderful thanks to your tutorial aora and I have an idea of ​​how to use the timer.

  19. M1M! Says:

    grax x code
    zi zirvio me!
    ○ ^ ^

  20. david says:

    thanks for this code I'm programming something bigger jejej
    thanks ...

  21. Wolverine says:

    excellent code especially useful to develop some application to monitor waiting times of a process

    Thanks a lot

  22. loquillo says:

    and if to count down as serious?

  23. Aldrin says:

    A very good example I had cast in c + + programming with my profresor but had no idea it was in visual input basic.net thanks for a very simple and yet very atractivo.Gracias

  24. john says:

    only a very good thing, in the reset button if you press it is set to 0 but still running out of time for that alone you would have to put

    Btn_reset_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_reset.Click
    Timer1.Enabled = False
    time = 0
    minute = 0
    second = 0
    millisecond = 0
    printTime ()
    End Sub

  25. andres said:

    Hi grace for the contribution

    I want to know how to adapt to ASP with VB since by using the code page is doing postback

    Thanks

  26. alfras says:

    many thanks, I was looking

  27. MHESSHY says:

    VERY GOOD INFORMATION saved me from A TROUBLE
    ..
    ..
    THANKS

  28. gatiaz says:

    Thank you very much for the contributions. They are very clear and very good.

  29. Amhed Elio says:

    Very good contribution, is what I was looking for!
    thanks

  30. danilo says:

    Can you tell me as you did with the label1.refresh ()
    Thank you.

Comment