限制文本框中的值 [英] restrict values in a textbox
本文介绍了限制文本框中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何限制vb.net中的文本框条目,使其仅接受数字键和退格键以更正任何错误.
我正在使用此代码:
how to restrict textbox entry in vb.net so that it accepts only numerical key and backspace to correct any error.
I am using this code:
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
Dim allowedChars As String = "0123456789"
If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
e.Handled = True
End If
End Sub
但不包括退格编码.
我还需要允许退格键.所以请帮忙.
谢谢!
but its not including the backspace coding.
I need to allow backspace also. So please help.
Thanks!
推荐答案
请检查以下修改后的代码:
Please check the following modified code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim allowedChars As String = "0123456789"
If e.KeyChar <> ControlChars.Back Then
If allowedChars.IndexOf(e.KeyChar) = -1 Then
''Invalid Character
e.Handled = True
End If
End If
End Sub
在If allowedChars.IndexOf(e.KeyChar) = -1 Then
之前添加此If Asc(e.KeyChar) = Asc(Keys.Back) Then Exit Sub
.
这篇关于限制文本框中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文