2011-08-30 81 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 
相關問題