2014-03-29 138 views
0

我有一個用戶輸入「學費」的文本框。我只需要用戶輸入數字數據。由於字符串數據不可能進行計算。但是用戶可能會輸入文字,如'$ 50'。驗證文本框中輸入的文本是否爲數字

我不喜歡使用MASKED_text_box因爲它顯示了 '_ _' 文本框內。 我能做什麼?請幫忙。

+0

你想讓'$'和數字olny啊? – Sathish

+0

也不是$也。只有數字。如果用戶輸入$,則應該通知他。 – user3057202

+0

如果用戶輸入0-9以外的字符時「沒有輸入內容」會很好 – user3057202

回答

0

嘗試像這樣

Private Sub TextBox4_KeyPress(ByVal sender As System.Object, ByVal e As 
System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress 

    If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _ 
     Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then 
     e.Handled = True 
    End If 

End Sub 
0

如果他們想打退格?

Private Sub tb_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tb.KeyPress 
Select Case Convert.ToInt32(e.KeyChar) 
    Case 48 To 57 
    'numbers are allowed 
    Case Keys.Back 
    'ok to hit the back space 
    Case Keys.Enter 
    'handle an enter key 
    e.Handled = True 
    Case Else 
    e.Handled = True 
End Select 
End Sub