Click to See Complete Forum and Search --> : Count Down
mejaz
March 3rd, 2007, 04:04 AM
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
laitinen
March 3rd, 2007, 05:14 AM
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.
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
laitinen
March 5th, 2007, 09:59 AM
Some modifications to the code in last post;
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
HanneSThEGreaT
March 6th, 2007, 01:46 AM
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 :
timeDiff = timeEnd.Subtract(DateTime.Now)
laitinen
March 6th, 2007, 02:03 AM
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 :
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
mejaz
March 6th, 2007, 06:50 AM
Thanks laitinen and HanneSThEGreaT.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.