设置一个只接受一定时间范围的文本框

Set up a TextBox that accepts only a range of times

我需要在文本框中插入格式为 hh:mm/hh:mm 的时间间隔(例如,08:00/13:0014:00/18:00)以设置特定用户的接收时间。

如何在输入的时间上也输入一个控件(如果我输入25:60/70:90,肯定不会被接受,因为时间表不存在)。提前致谢!

您可以使用 Regular Expressions 进行此验证。您需要使用这样的模式:

(?:[0-1]\d|2[0-3]):[0-5]\d\/(?:[0-1]\d|2[0-3]):[0-5]\d

Try it online.

首先,添加以下引用:

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

另一种选择是使用 TimeSpan.TryParseExact。这将为您提供相同的结果,并且还允许您轻松获得时间值 TimeSpans,以便您可以将它们用于进一步的计算或将它们存储在某个地方,例如。

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 而不是文本框。

首先,将 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

这是一个示例,如果在您指定的格式中找到四个有效时间,则将 return 一个 List(Of TimeSpan)。它的设计有点笨拙,因此您可以准确地看到为确保所有部分都有效而采取的步骤。将这四次作为 TimeSpans 返回后,您可以进一步处理它们以查看它们作为 "shift":

是否有意义
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