CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Count Down

  1. #1
    Join Date
    Feb 2005
    Posts
    131

    Question Count Down

    I am using vb.net 2003.

    How can i make a count down in vb.net?

    I am using 2 textboxes and a timer and a checkbox.

    TxtHours , Txtminutes

    It should work like this that if user enter 0 in hours and 5 in minutes box and as soon user will select checkbox then it will start cont down.

    00:05:00
    00:04:59
    00:04:58
    .
    .
    .
    And up to

    00:00:00

    Thanks in advance

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Count Down

    Hi,

    When the user clicks the checkbox you save the current time, and based on this you calculate the end time from the user input.

    Then your timer should be responsible to update a label where you show the countdown. This timer should get the current time each time it is triggered and update the label based on the difference to the end time.

    Code:
    Public Class Form1
        Dim timeEnd As DateTime
        Dim timeDiff As TimeSpan
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            timeEnd = DateTime.Now
    
            Dim hour As Double = System.Convert.ToDouble(txtHour.Text)
            Dim minute As Double = System.Convert.ToDouble(txtMinute.Text)
    
            timeEnd = timeEnd.AddHours(hour)
            timeEnd = timeEnd.AddMinutes(minute)
    
            Timer1.Start()
    
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            timeDiff = timeEnd - DateTime.Now
    
            txtRemaining.Text = timeDiff.ToString
    
    
        End Sub
    End Class
    txtHour,txtMinute and txtRemaining is textboxes.
    Regards,

    Laitinen
    Last edited by laitinen; March 3rd, 2007 at 06:34 AM.

  3. #3
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Count Down

    Some modifications to the code in last post;

    Code:
    Public Class Form1
        Dim timeEnd As DateTime
        Dim timeDiff As TimeSpan
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            timeEnd = DateTime.Now
    
            Dim hour As Double = System.Convert.ToDouble(txtHour.Text)
            Dim minute As Double = System.Convert.ToDouble(txtMinute.Text)
            Dim second As Double = System.Convert.ToDouble(txtSecond.Text)
    
            timeEnd = timeEnd.AddHours(hour)
            timeEnd = timeEnd.AddMinutes(minute)
            timeEnd = timeEnd.AddSeconds(Second)
    
            Timer1.Start()
    
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            timeDiff = timeEnd - DateTime.Now
    
            Dim output As TimeSpan = New TimeSpan(timeDiff.Hours, timeDiff.Minutes, timeDiff.Seconds)
            txtRemaining.Text = output.ToString
    
            If (timeDiff.Ticks < 0) Then
    
                Timer1.Stop()
                MessageBox.Show("ZERO!!!!")
            End If
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            txtMinute.Text = 0
            txtHour.Text = 0
            txtSecond.Text = 0
        End Sub
    End Class
    Here I have added another textbox called txtSecond. Also I have set the initial values of hours,minutes and seconds to 0, and stopped the timer when it reaches zero.

    Regards,

    Laitinen
    Last edited by laitinen; March 5th, 2007 at 11:58 AM.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Count Down

    Quote Originally Posted by laitinen
    timeDiff = timeEnd - DateTime.Now
    Just a note here, in VB.NET 2003, you cannot do this, you will have to use the Subtract Method of the Date object to subtract the particular date. Do it like :
    Code:
    timeDiff = timeEnd.Subtract(DateTime.Now)

  5. #5
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Count Down

    Quote Originally Posted by HanneSThEGreaT
    Just a note here, in VB.NET 2003, you cannot do this, you will have to use the Subtract Method of the Date object to subtract the particular date. Do it like :
    Code:
    timeDiff = timeEnd.Subtract(DateTime.Now)
    Yes I am sorry that I forgot to specify that this was VB.NET 2005 code.

    Anyway, good that you corrected me!

    Regards,

    Laitinen

  6. #6
    Join Date
    Feb 2005
    Posts
    131

    Thumbs up Re: Count Down

    Thanks laitinen and HanneSThEGreaT.

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

Featured