我对24小时制格式文本有疑问。
我要求的格式总是如下00:00
如果输入第二个数字,我怎么能使“:”出现,因为这需要将小时与分钟分开,以及如何强制为任何数字(包括9?)强制显示0?
例如:00,01,02,03,04,05,06,07,08,09。
这些零是必需的,但用户可能会忘记将它们输入文本框?
答案 0 :(得分:0)
使用MaskedTextBox。您可以将.Mask
属性设置为"00:00"
,当用户输入时间时,该框将显示为_ _ : _ _
(因此它不会出现在第二个数字后面,但它会永远作为指导)。
要验证小时数在0-23之间以及0-59之间的分钟数,请使用Validating
方法(有关详细信息,请参阅[此MSDN帖子])。
答案 1 :(得分:0)
如果你想要:也是,请使用这个版本。如果用户键入任何数字,请添加0> 3(0,1和2可能是2位数小时)
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length() = 2 Then
TextBox1.Text &= ":"
TextBox1.SelectionStart = TextBox1.Text.Length()
End If
If TextBox1.Text.Length() = 1 AndAlso TextBox1.Text > "2" Then
TextBox1.Text = "0" & TextBox1.Text
TextBox1.SelectionStart = TextBox1.Text.Length()
End If
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = vbBack AndAlso TextBox1.Text.Length() = 3 Then
TextBox1.Text = TextBox1.Text(0)
e.Handled = True
End If
End Sub