Results 1 to 7 of 7

Thread: create visible countdown

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2000
    Location
    USA
    Posts
    37

    Post

    How can you make a counter start at a number, count down to 0(or any #) and display this countdown through a label.

  2. #2
    Guest
    Code:
    Private Declare Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)
    
    Private Sub Command1_Click()
         For i = 1000 To 0 Step -1
              Label1.Caption = i
              Sleep 1: DoEvents
         Next i
    End Sub

  3. #3
    Guest
    Why do all that when a timer control can be used?

    Anyway, doesnt the sleep function freeze the entire application for the set time?

  4. #4
    Lively Member
    Join Date
    Oct 2000
    Location
    Singapore
    Posts
    98
    Yeah, u could simply just put the code insidde the timer. (with the label.caption and counter values)

  5. #5
    Guest
    Why do all what? It ain't much.
    I like my way. But if you insist...

    Code:
    Private Sub Timer1_Timer()
    Label1.Caption = Val(Label1.Caption) - 1
    End Sub
    And sleep didn't seem to freeze me. Perhaps, the DoEvents came in?

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2000
    Location
    USA
    Posts
    37
    all i wanted to have was a countdown that could be shown through a label. None of the code worked. I know how to countdown starting at 0. I want to start at five and countdown to 0. Please help!

  7. #7
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523
    Code:
    '//Add Timer and Label to the Form
    Option Explicit
    
    Private Sub Command1_Click()
        Timer1.Enabled = True
        Timer1.Interval = 1000
    End Sub
    
    Private Sub Form_Load()
        Label1.Caption = 5
    End Sub
    
    Private Sub Timer1_Timer()
        If Label1.Caption <> 0 Then
            Label1.Caption = Label1.Caption - 1
        Else
            MsgBox "End of countdown"
            Timer1.Enabled = False
        End If
    End Sub
    '//HTH
    Second version (it will stop the countdown at exactly zero not one second after (like previous code)
    Code:
    '//Add Timer and Label to the Form
    Option Explicit
    
    Private Sub Command1_Click()
        Timer1.Enabled = True
        Timer1.Interval = 1000
    End Sub
    
    Private Sub Form_Load()
        Label1.Caption = 5
    End Sub
    
    Private Sub Timer1_Timer()
        If Label1.Caption > 1 Then
            Label1.Caption = Label1.Caption - 1
        ElseIf Label1.Caption = 1 Then
            Timer1.Interval = 1
            Label1.Caption = 0
        ElseIf Label1.Caption = 0 Then
            MsgBox "End of countdown"
            Timer1.Enabled = False
        End If
    End Sub
    '//HTH
    [Edited by QWERTY on 10-29-2000 at 01:09 AM]

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