Results 1 to 8 of 8

Thread: RE: Countdown Timer in a Console Application

Hybrid View

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

    Re: Countdown Timer in a Console Application

    That's pretty tricky... It took me almost 30 minutes to get it working right.
    The trick is that, to remove 1 character, you ned to write a backspace + space + backspace.... Then when your value decrease from let's say 100 to 99, you need an adjustment so that it "erase" the correct amount of character on the screen... Anyway, here is the working code:
    Code:
    Module Module1
    
        Private WithEvents CountDownTimer As System.Timers.Timer
        Private countDownValue As Integer = 1200
    
        Sub Main()
            CountDownTimer = New System.Timers.Timer()
            With CountDownTimer
                .AutoReset = True
                .Interval = 100
            End With
            Console.Write("Start counting down... " & countDownValue)
            CountDownTimer.Start()
    
            'Prevent the application to close
            Console.ReadLine()
        End Sub
    
        Private Sub CountDownTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles CountDownTimer.Elapsed
            Static lastDigitCount As Integer = countDownValue.ToString.Length
            countDownValue -= 1
            Dim currentDigitCount As Integer = countDownValue.ToString.Length
            'Adjustment to character erasing...
            If lastDigitCount > currentDigitCount Then
                Console.Write(ChrW(8) & ChrW(32) & ChrW(8))
                lastDigitCount = currentDigitCount
            End If
    
            'Erase the old value and write the new value
            If countDownValue > 0 Then
                For i As Integer = 0 To currentDigitCount - 1
                    Console.Write(ChrW(8) & ChrW(32) & ChrW(8))
                Next
                Console.Write(countDownValue)
            Else
                CountDownTimer.Stop()
                Console.Write(ChrW(8) & ChrW(32) & ChrW(8) & countDownValue & Environment.NewLine)
                Console.Write("Press Enter key to exit...")
            End If
        End Sub
    End Module
    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 -

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Location
    Manchester, CT
    Posts
    317

    Re: Countdown Timer in a Console Application

    Cool thanks for all of the responses... I cant wait to put this in tomorrow and see it work
    "I dont even see the code anymore... I just see Blonde, Brunette, Redhead..."

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