Results 1 to 4 of 4

Thread: Timers...

  1. #1

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Question Timers...

    Nevermind
    Last edited by jbailey01; Jul 31st, 2012 at 03:28 PM.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Timers...

    You don't need a timer for this. You can use a StopWatch, or a simple datetime variable. Start the stopwatch or set the variable to the current datetime in form load. Later, when you need to know how much time has elapsed, you read the Elapsed property of the stopwatch or subtract the saved datetime from the current datetime.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Timers...

    Try

    Code:
    Private i As Integer
        
        Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            With Timer1
                .Interval = 1000 '1 Second
                .Start()
            End With
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            i += 1
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            MessageBox.Show("It's been " & i.ToString & " seconds since the form's loaded", Me.Text, MessageBoxButtons.OK)
        End Sub
    Edit - Why just edit your op to "nevermind"? That's annoying. But stanav's correct, you should use a stopwatch. For those of you wondering, the origional post was "How do I keep track of the time lapsed since my formload?".


    Edit 2 - This is the samething using a stopwatch:
    Code:
    Dim sw As New Stopwatch
    
        Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            sw.Start()
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim i As Integer = CInt(sw.ElapsedMilliseconds)
            i = CInt(i * 0.001)
    
            MessageBox.Show("It's been " & i.ToString & " seconds since the form's loaded", Me.Text, MessageBoxButtons.OK)
        End Sub
    Last edited by dday9; Jul 31st, 2012 at 03:48 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Re: Timers...

    Thank you guys. I thought my code was wrong, but it was another peice of the pie. Very much appreciated!!

Tags for this Thread

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