仅在文本框中输入数字

编程入门 行业动态 更新时间:2024-10-28 12:18:23
本文介绍了仅在文本框中输入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何仅在文本框中输入数字?如果我按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.

更多推荐

仅在文本框中输入数字

本文发布于:2023-11-23 14:57:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   框中输入   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!