Results 1 to 6 of 6

Thread: Timer

  1. #1

    Thread Starter
    Lively Member Spie's Avatar
    Join Date
    Jul 2000
    Location
    On a very small coconut somewhere near Mars
    Posts
    126
    How do I make a timer?
    I want it to count down from 500 until it gets to 0.
    I have this code in my formload:
    ----------------
    Do
    Label3.caption = label3 - 1
    loop
    ----------------
    VB gets pissed off and crashes though... Am I doing something wrong?

  2. #2
    Lively Member
    Join Date
    Sep 2000
    Posts
    126

    Yep

    The best way to do it is to use the timer object, though most people hate it, the code would be:

    Code:
    Dim intcount As Integer
    
    Option Explicit
    
    Private Sub Form_Load()
        Timer1.Interval = 1000
        Timer1.Enabled = True
        intcount = 501
    End Sub
    
    Private Sub Timer1_Timer()
        intcount = intcount - 1
        If count <> 1 Then
            Label1.Caption = intcount
        Else
            Timer1.Enabled = False
        End If
    End Sub
    Hope this helps

  3. #3

    Thread Starter
    Lively Member Spie's Avatar
    Join Date
    Jul 2000
    Location
    On a very small coconut somewhere near Mars
    Posts
    126
    Thanks

  4. #4

    Thread Starter
    Lively Member Spie's Avatar
    Join Date
    Jul 2000
    Location
    On a very small coconut somewhere near Mars
    Posts
    126
    Um... How do I make it stop at 0 and not go to the - numbers?

  5. #5
    Guest
    You can also do it without a Timer.
    Code:
    Sub CountDown(StartValue As Integer, lbl As Label)
    
        lbl = StartValue
        
        Do Until lbl = 0
            Start = Timer
            
            Do While Timer < Start + 1
            DoEvents
            Loop
            
            lbl = lbl - 1
        Loop
        
    End Sub
    
    Private Sub Command1_Click()
        CountDown 10, Label1
    End Sub

  6. #6
    Lively Member
    Join Date
    Sep 2000
    Posts
    126

    If statement

    Use and if statement to tell it the program it must be more than 0...

    Code:
    Dim intcount As Integer
    
    Option Explicit
    
    Private Sub Form_Load()
        Timer1.Interval = 1000
        Timer1.Enabled = True
        intcount = 501
    End Sub
    
    Private Sub Timer1_Timer()
        intcount = intcount - 1
        If count > 0 Then
            Label1.Caption = intcount
        Else
            Timer1.Enabled = False
        End If
    End Sub

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