If condition
-
How can I shorten this lengthy code, which is not right as well? Though I want to write it using if condition / select case instead of using DateTimePicker. It starts from 7:00 AM, 7:15 AM, 7:30 AM, 7:45 AM, 8:00 AM ................So on until 7:00 PM.
Private Sub txtStartTime_Leave(sender As Object, e As EventArgs) Handles txtStartTime.Leave
If txtStartTime.Text = 7 Then
txtStartTime.Text = "7:00 AM"
ElseIf txtStartTime.Text = 715 Then
txtStartTime.Text = "7:15 AM"
ElseIf txtStartTime.Text = 730 Then
txtStartTime.Text = "7:30 AM"
ElseIf txtStartTime.Text = 745 Then
txtStartTime.Text = "7:45 AM"
ElseIf txtStartTime.Text = 8 Then
txtStartTime.Text = "8:00 AM"
ElseIf txtStartTime.Text = 815 Then
txtStartTime.Text = "8:15 AM"
ElseIf txtStartTime.Text = 830 Then
txtStartTime.Text = "8:30 AM"
ElseIf txtStartTime.Text = 845 Then
txtStartTime.Text = "8:45 AM"
End If
End Sub -
How can I shorten this lengthy code, which is not right as well? Though I want to write it using if condition / select case instead of using DateTimePicker. It starts from 7:00 AM, 7:15 AM, 7:30 AM, 7:45 AM, 8:00 AM ................So on until 7:00 PM.
Private Sub txtStartTime_Leave(sender As Object, e As EventArgs) Handles txtStartTime.Leave
If txtStartTime.Text = 7 Then
txtStartTime.Text = "7:00 AM"
ElseIf txtStartTime.Text = 715 Then
txtStartTime.Text = "7:15 AM"
ElseIf txtStartTime.Text = 730 Then
txtStartTime.Text = "7:30 AM"
ElseIf txtStartTime.Text = 745 Then
txtStartTime.Text = "7:45 AM"
ElseIf txtStartTime.Text = 8 Then
txtStartTime.Text = "8:00 AM"
ElseIf txtStartTime.Text = 815 Then
txtStartTime.Text = "8:15 AM"
ElseIf txtStartTime.Text = 830 Then
txtStartTime.Text = "8:30 AM"
ElseIf txtStartTime.Text = 845 Then
txtStartTime.Text = "8:45 AM"
End If
End SubTry something like this:
Dim time As Integer
If Integer.TryParse(txtStartTime.Text, time) ThenDim hour As Integer = 0, minutes As Integer = 0 If 7 <= time AndAlso time <= 19 Then hour = time minutes = 0 Else If 700 <= time AndAlso time <= 1900 Then hour = time \\ 100 minutes = time Mod 100 End If If hour <> 0 Then ' Round to the nearest 15 minutes: minutes = 15 \* Math.Round(minutes / 15) If minutes = 60 Then hour += 1 minutes = 0 End If Dim d As New Date(1, 1, 1, hour, minutes, 0) txtStartTime.Text = d.ToString("hh:mm tt") End If
End If
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
How can I shorten this lengthy code, which is not right as well? Though I want to write it using if condition / select case instead of using DateTimePicker. It starts from 7:00 AM, 7:15 AM, 7:30 AM, 7:45 AM, 8:00 AM ................So on until 7:00 PM.
Private Sub txtStartTime_Leave(sender As Object, e As EventArgs) Handles txtStartTime.Leave
If txtStartTime.Text = 7 Then
txtStartTime.Text = "7:00 AM"
ElseIf txtStartTime.Text = 715 Then
txtStartTime.Text = "7:15 AM"
ElseIf txtStartTime.Text = 730 Then
txtStartTime.Text = "7:30 AM"
ElseIf txtStartTime.Text = 745 Then
txtStartTime.Text = "7:45 AM"
ElseIf txtStartTime.Text = 8 Then
txtStartTime.Text = "8:00 AM"
ElseIf txtStartTime.Text = 815 Then
txtStartTime.Text = "8:15 AM"
ElseIf txtStartTime.Text = 830 Then
txtStartTime.Text = "8:30 AM"
ElseIf txtStartTime.Text = 845 Then
txtStartTime.Text = "8:45 AM"
End If
End Sub -
Try something like this:
Dim time As Integer
If Integer.TryParse(txtStartTime.Text, time) ThenDim hour As Integer = 0, minutes As Integer = 0 If 7 <= time AndAlso time <= 19 Then hour = time minutes = 0 Else If 700 <= time AndAlso time <= 1900 Then hour = time \\ 100 minutes = time Mod 100 End If If hour <> 0 Then ' Round to the nearest 15 minutes: minutes = 15 \* Math.Round(minutes / 15) If minutes = 60 Then hour += 1 minutes = 0 End If Dim d As New Date(1, 1, 1, hour, minutes, 0) txtStartTime.Text = d.ToString("hh:mm tt") End If
End If
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you so much for your kind help.