以下内容允许您测试特定的键值(来自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
Run Code Online (Sandbox Code Playgroud)
如果这不是您要找的,请尝试该isNumeric()功能.
只是为了帮助,我有一个片段,我正在制作一个可能有用的计算器,它允许删除,退格,数字和'.' 焦炭.您还可以添加对函数的调用,以便在按Enter键时处理文本框.
Private Sub Box_Input_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles Box_Input.KeyDown
If Char.IsDigit(Chr(e.KeyValue)) Or _
Chr(e.KeyValue) = "¾"c Or _
e.KeyData = Keys.Delete Or _
e.KeyData = Keys.Back Then
If Chr(e.KeyValue) = "¾"c Then
If Box_Input.Text.Contains(".") Then
e.SuppressKeyPress = True
Else
e.SuppressKeyPress = False
End If
End If
ElseIf e.KeyData = Keys.Enter Then
`State a call to function for when Enter is pressed`
Else
e.SuppressKeyPress = True
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
此代码允许输入所有数字,后退空格和删除键以及仅允许一个'.' 在文中.一个想法可能是添加TAB功能以允许Tab停止.
| 归档时间: |
|
| 查看次数: |
58572 次 |
| 最近记录: |