我需要在文本框中插入格式为hh:mm/hh:mm
的时间间隔(例如,08:00/13:00
,14:00/18:00
)来设置特定用户的接收时间。
如何通过输入对输入时间的控制来执行此操作(如果我输入25:60/70:90
,则不能接受它,因为时间表不存在)。提前致谢!
另一种选择是使用TimeSpan.TryParseExact
。这将为您提供相同的结果,并且它还允许您轻松地将时间值作为TimeSpan
s获取,以便您可以将它们用于进一步计算或将其存储在某处。
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim validRange As Boolean = False
Dim time1, time2 As TimeSpan
Dim timeRanges As String() = TextBox1.Text.Split("/"c)
If timeRanges.Length = 2 Then
If TimeSpan.TryParseExact(timeRanges(0), "hh\:mm",
Globalization.CultureInfo.InvariantCulture, time1) AndAlso
TimeSpan.TryParseExact(timeRanges(1), "hh\:mm",
Globalization.CultureInfo.InvariantCulture, time2) Then
validRange = True
End If
End If
If validRange Then
' Use `time` and `time2` for anything you want.
Else
' TODO: Indicate to the user that they entered an invalid input.
e.Cancel = True
End If
End Sub
如果您不想手动键入:
和/
,则可以使用MaskedTextBox
而不是TextBox。
首先,在您的表单上添加MaskedTextBox后,将其Mask
属性设置为00:00/00:00
:
然后你可以调整上面的代码来使用MaskedTextBox,如下所示:
Private Sub MaskedTextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MaskedTextBox1.Validating
Dim validRange As Boolean = False
Dim time1, time2 As TimeSpan
Dim timeRanges As String() = MaskedTextBox1.Text.Split("/"c)
If MaskedTextBox1.MaskCompleted Then
If TimeSpan.TryParseExact(timeRanges(0), "hh\:mm",
Globalization.CultureInfo.InvariantCulture, time1) AndAlso
TimeSpan.TryParseExact(timeRanges(1), "hh\:mm",
Globalization.CultureInfo.InvariantCulture, time2) Then
validRange = True
End If
End If
If validRange Then
' Use `time` and `time2` for anything you want.
Else
' TODO: Indicate to the user that they entered an invalid input.
e.Cancel = True
End If
End Sub
您可以使用Regular Expressions进行此验证。你需要使用像这样的模式:
(?:[0-1]\d|2[0-3]):[0-5]\d\/(?:[0-1]\d|2[0-3]):[0-5]\d
首先,添加以下参考:
Imports System.Text.RegularExpressions
然后,假设这是一个WinForms项目,你需要像这样使用TextBox.Validating
事件:
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Const pattern As String = "(?:[0-1]\d|2[0-3]):[0-5]\d\/(?:[0-1]\d|2[0-3]):[0-5]\d"
If Not Regex.IsMatch(TextBox1.Text, pattern) Then
' TODO: Indicate to the user that they entered an invalid input.
e.Cancel = True
End If
End Sub
这是一个示例,如果以您指定的格式找到四个有效时间,将返回List(Of TimeSpan)
。根据设计,它有点笨重,因此您可以准确地看到确保所有部件都有效的步骤。一旦你将这四次作为TimeSpans返回,你可以进一步处理它们,看看它们是否有意义作为“转变”:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim times As List(Of TimeSpan) = ParseShift(TextBox1.Text)
If times.Count = 4 Then
Label1.Text = "Valid Times Found"
' perform some kind of validation on them?
For Each ts As TimeSpan In times
Debug.Print(ts.ToString)
Next
Else
Label1.Text = "Invalid Times and/or Shift Description"
End If
End Sub
Private Function ParseShift(ByVal input As String) As List(Of TimeSpan)
Dim values() As String = input.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
If values.Length = 2 Then
Dim firstHalf() As String = values(0).Split("/".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
Dim secondHalf() As String = values(1).Split("/".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
If firstHalf.Length = 2 AndAlso secondHalf.Length = 2 Then
Dim strTimes As New List(Of String)
strTimes.AddRange(firstHalf)
strTimes.AddRange(secondHalf)
Dim ts As TimeSpan
Dim times As New List(Of TimeSpan)
For Each strTime As String In strTimes
If TimeSpan.TryParseExact(strTime.Trim, "hh\:mm", Globalization.CultureInfo.InvariantCulture, ts) Then
times.Add(ts)
End If
Next
If times.Count = 4 Then
Return times
End If
End If
End If
Return New List(Of TimeSpan)
End Function