仅在文本框中输入数字 [英] input number only in a textbox
本文介绍了仅在文本框中输入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何仅在文本框中输入数字?如果我按Backspace键或Delete键,还可以删除字符吗?我在Internet上尝试了一些代码,但是如果按退格键或Delete键,它并不会删除字符.
您能给我一些代码吗?
How to input number only in a text box? And also can remove a character if I press the backspace key or delete key? I tried some code in the internet but it didn''t remove a character if I press the backspace or delete key.
can you give me some code...
推荐答案
尝试一下:
Try this:
Private Sub NumberValidation(sender As Object, e As KeyPressEventArgs)
If (e.KeyChar >= 48 AndAlso e.KeyChar <= 57) OrElse e.KeyChar = 8 Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
您应该自己写自定义文本框类,如此处的链接:
http://www.daniweb.com/software-development/vbnet/code/353389/numberbox-textbox-that-takes-only-number-input [
You should write you own custom textbox class, as in the link here:
http://www.daniweb.com/software-development/vbnet/code/353389/numberbox-textbox-that-takes-only-number-input[^]
This allows handling the keypress event and filtering out any character that is unwanted. For the delete and backspace key: They are normally working in textboxes without any added code.
If Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Or Asc(e.KeyChar) = 8 Then
e.Handled = False
Else
e.Handled = True
End If
我尝试了这段代码,它可以正常工作.
感谢所有人.
I tried this code and it works.
thanks to all.
这篇关于仅在文本框中输入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文