验证在文本框中输入的文本是否为数字

时间:2014-03-29 03:10:37

标签: vb.net

我有一个文本框,用户可以在其中输入学费'。我需要用户只输入数字数据。因为字符串数据无法进行计算。但是,用户可能会输入“$ 50'。

等文字

我不喜欢使用MASKED_text_box,因为它显示' _ _ '在文本框内。 我能做什么 ?请帮忙。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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
相关问题