A little problem with countdown timer

daybraker

Member
Joined
Jun 30, 2011
Messages
8
Programming Experience
1-3
I was trying to edit this countdown timer code but it returns ans error maybe somebody can figure this out. It is a simple countdown you select a date and time and it counts back to that date ant time. The error Conversation from string to date is not valid.

Public Class Form1
Dim atgal As Date
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
atgal = FormatDateTime(Me.DateTimePicker1.Text & " " & _
Me.DateTimePicker2.Text, DateFormat.GeneralDate) This part returns the error
Me.Timer2.Enabled = True
End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Dim sena As Date = Date.Now
Dim nauja As Date = atgal
Dim skirtumas As TimeSpan = sena.Subtract(nauja)
Me.Label3.Text = "Days" & skirtumas.Days & "Time" & skirtumas.Hours & ":" & skirtumas.Minutes & ":" & skirtumas.Seconds
If skirtumas.Days = 0 And skirtumas.Hours = 0 And skirtumas.Minutes = 0 And skirtumas.Seconds = 0 Then
Me.Timer1.Enabled = False
MessageBox.Show("Session reached", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Me.Label3.Text = "Days 0 Time 00:00:00"
Me.Timer1.Enabled = False
End Sub

End Class
 
This line of code:
VB.NET:
atgal = FormatDateTime(Me.DateTimePicker1.Text & " " & _
Me.DateTimePicker2.Text, DateFormat.GeneralDate)
makes no sense at all. The point of the FormatDateTime method is to convert a DateTime to a String in a specific format, you are starting with Strings and assigning the result to a DateTime variable, which is the exact opposite. What are you actually trying to achieve?

If you want to get a Date from a DateTimePicker then you simply get its Value property. You should pretty much always be using the Value property and never the Text.
 
I want for the program to count down to a specific date and time and the countdown output/rezult should be visible in the label. In other terms you select the date you select the time and the program counts down back to it.
 
Only a single DateTimePicker is needed, the Date value represents both the date and the time. The date can be selected in the dropdown, and time be set in the textbox area (Format set to Time). Having a custom display format is also possible if you want all that in the textbox area. The selected date and time value is retrieved as explained by Value property.
 
Back
Top