本文介绍了仅在文本框中输入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何仅在文本框中输入数字?如果我按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
您应该自己写自定义文本框类,如此处的链接: www.daniweb/software-development/vbnet/code/353389/numberbox-textbox-that-takes-only-number-input [ ^ ] 这允许处理按键事件并过滤掉不需要的任何字符.对于Delete和Backspace键:它们通常可以在文本框中运行,而无需添加任何代码. You should write you own custom textbox class, as in the link here: www.daniweb/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.
更多推荐
仅在文本框中输入数字
发布评论