如何允许用户在 vb.net 的文本框中只输入数字?
问题描述:
如何防止用户输入字符但只输入数字和."在 vb.net 2005 的文本框中?
How can I prevent the user to enter characters but only numbers and '.' in a textbox in vb.net 2005?
答
以下将允许您测试特定的键值(来自 http://www.daniweb.com/forums/thread127299.html):
The following will allow you to test for specific key values (from http://www.daniweb.com/forums/thread127299.html):
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
End Sub
如果这不是您要查找的内容,请尝试使用 isNumeric()
函数.
If this isn't what you are looking for, try the isNumeric()
function.