文本框只接受数字 [英] textbox accept only digits
本文介绍了文本框只接受数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim allowedChars As String = "0123456789$,"
If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
e.Handled = True
End If
End Sub
这段代码只接受数字,它的作用就像一个魅力,但如果我输入了错误的数字,它不允许我使用键盘上的删除或退格键如何解决这个问题?
this code accept only digits and its working like a charm but if i typed a wrong number its not allowing me to use the delete or the backspace on the keyboard how to solve this problem ?
推荐答案
这可以追溯到很久以前,终端仍然被使用的时候.您还必须允许使用退格字符.把它放在你的代码前面:
This goes back a long time, back when terminals were still used. You'll also have to allow the backspace character. Put this in the front of your code:
If e.KeyChar = Chr(8) Then Exit Sub
Delete 键不需要任何帮助,已经可以使用了.
The Delete key doesn't need any help, that already works.
这篇关于文本框只接受数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文