Results 1 to 10 of 10

Thread: VB Timer

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    13

    VB Timer

    Hey, ive got this VB project and im trying to put in a timer.
    So when u click a button a timer will start from zero and count up by 1 second. When the button is clicked again the timer will stop and a msg box will display how many seconds and milliseconds it took.
    I also understand that you need a label for the time to be displayed in.
    If anyone can help me out with this thatd be great, cheers
    My VB Code = Very Basic Code

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: VB Timer

    It would be appreciated if you could specify which version of VB you're using in future. If it's VB 2005 then you already have a Stopwatch class available to you. If its earlier then I have posted a Stopwatch class in the CodeBank. You don't actually need a Timer to calculate elapsed time at all. If you want to display the elapsed time as it increases then you would need a Timer for that. Both the .NET 2.0 Stopwatch class and my Stopwatch class have Elapsed properties. If you add a Timer to your form with an Interval of 1000 (milliseconds), you can then call Start on the Timer and the Stopwatch at the same time. In the Timer's Tick event handler you would then do something like this:
    VB Code:
    1. Public Class Form1
    2.  
    3.     Dim myStopwatch As New Stopwatch
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Me.Timer1.Start()
    7.         myStopwatch.Start()
    8.     End Sub
    9.  
    10.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    11.         Dim elapsedTime As TimeSpan = Me.myStopwatch.Elapsed
    12.  
    13.         Me.Label1.Text = String.Format("{0:d2}:{1:d2}:{2:d2}", _
    14.                                        Convert.ToInt32(Math.Floor(elapsedTime.TotalHours)), _
    15.                                        elapsedTime.Minutes, _
    16.                                        elapsedTime.Seconds)
    17.     End Sub
    18.  
    19. End Class
    The time displayed will not be perfect though. Because of the nature of Windows applications and the Timer class there will be times where the same time will be displayed for two seconds and then skip a second. You will get a slightly more accurate result with this:
    VB Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.     Dim elapsedTime As TimeSpan = Me.myStopwatch.Elapsed
    3.  
    4.     Me.Label1.Text = String.Format("{0:d2}:{1:d2}:{2:d2}", _
    5.                                    Convert.ToInt32(Math.Floor(elapsedTime.TotalHours)), _
    6.                                    elapsedTime.Minutes, _
    7.                                    elapsedTime.Seconds + Convert.ToInt32(Math.Round(elapsedTime.Milliseconds / 1000)))
    8. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    New Member Timmy_J's Avatar
    Join Date
    Jul 2006
    Posts
    8

    Re: VB Timer

    Hey, yeah i had this same problem to, and this code really helped,
    however is it possible to have just seconds and milliseconds, and be able to view the milliseconds counting upwards as well.
    I use visual basic express, so any help would be cool

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: VB Timer

    Quote Originally Posted by Timmy_J
    Hey, yeah i had this same problem to, and this code really helped,
    however is it possible to have just seconds and milliseconds, and be able to view the milliseconds counting upwards as well.
    I use visual basic express, so any help would be cool
    You're unlikely to get a very acceptable result if you want to update a Label every millisecond because there will almost certainly be pauses in it, but you can give it a go. The principle is exactly the same. You just have to change the Timer Interval and the way you format the output string.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    New Member Timmy_J's Avatar
    Join Date
    Jul 2006
    Posts
    8

    Re: VB Timer

    okay, yeah i tried but it didnt work out, lol
    thanks for your help anyway.
    It has really helped out in my program
    My software never has bugs. It just develops random features.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: VB Timer

    Quote Originally Posted by Timmy_J
    okay, yeah i tried but it didnt work out, lol
    thanks for your help anyway.
    It has really helped out in my program
    Actually I just tried it myself and it worked better than I expected. You couldn't hope to actually read the display as it's running anyway. If you were perfroming some other processor intensive tasks you may find that it became jerky but I guess we are used to that anyway. The new format would look like this:
    VB Code:
    1. Me.Label1.Text = String.Format("{0}.{1:d3}", _
    2.                                Convert.ToInt32(Math.Floor(elapsedTime.TotalSeconds)), _
    3.                                elapsedTime.Milliseconds)
    Last edited by jmcilhinney; Jul 20th, 2006 at 02:33 AM. Reason: Fixed format of seconds
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    New Member Timmy_J's Avatar
    Join Date
    Jul 2006
    Posts
    8

    Re: VB Timer

    Okay yeah, thats seems to work, thanks heaps
    My software never has bugs. It just develops random features.

  8. #8
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: VB Timer

    this looks like a homework assignment

  9. #9
    New Member Timmy_J's Avatar
    Join Date
    Jul 2006
    Posts
    8

    Re: VB Timer

    lol yeah it sorta is, but this is my first time ive used visual basic and actually had to make a prgram, lol so im a bit stumped lol
    My software never has bugs. It just develops random features.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: VB Timer

    Quote Originally Posted by maged
    this looks like a homework assignment
    Good point. Broke my own rules.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width