2011-08-30 85 views
0

我有一个关于24小时格式化文本的问题。格式化程序用户输入的24小时时间用户

我需要的格式始终如下00:00

我怎样才能让“:”似乎只要因为这是需要分开分钟小时进入第二个数字,也哪能迫使0出现在9以下的任何数字中?

例子是:00,01,02,03,04,05,06,07,08,09

这些零必需的,但可以将其输入到文本框中的用户被遗忘?

回答

0

使用MaskedTextBox。您可以将.Mask属性设置为"00:00",并且在用户输入时间(因此它不会出现在第二个数字后面,但始终在此处作为向导)时,该框将看起来像_ _ : _ _

要验证小时数在0-23和0-59的分钟数内,请使用Validating方法(有关详细信息,请参见[本MSDN文章])。

0

如果你想要:也可以使用这个版本。如果用户键入任意数字> 3(0,1和2可能是2位小时),则添加0。

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 
相关问题