使文本框仅接受数字 [英] make text box accept only numbers
本文介绍了使文本框仅接受数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助使文本框只接受数字。我用的是vb.net。第二个想法,如果它也可以接受小数和负数,那就太好了。
解决方案
简而言之,你可以尝试类似的东西。
私有 Sub TextBox1_KeyPress( ByVal sender As System。 Object , ByVal e As System.Windows.Forms.KeyPressEventArgs)句柄 TextBox1.KeyPress
如果 不 Char .IsDigit(e.KeyChar)和 不 字符 .IsControl(e.KeyChar)然后
e.Handled = True
结束 如果
结束 Sub
另见这些..
如何创建文本框只接受VB.Net中的数字和句号 [ ^ ]
Vb-net-need-text-box-only-accept-numbers [ ^ ]
操作方法允许使用r-to-enter-only-number-in-a-textbox-in-vb-net [ ^ ]
使用.Net提供的正则表达式验证器。
如果你不知道那是什么,请使用搜索引擎并开始学习。
在.net工具箱中,是控件NumericUpDown。它非常方便。
您可以将其设置为接受x长度或整数的小数。
访问它的值,X为整数= NumUpDown.Value。
I need help making a textbox accept only numbers. I use vb.net. On second thought, it would be nice if it can accept decimals and negative numbers, too.
解决方案
In short you can try something like..
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then e.Handled = True End If End Sub
Also see these..
How to create a text box that accepts only numbers and full stop in VB.Net[^]
Vb-net-need-text-box-to-only-accept-numbers[^]
How-to-allow-user-to-enter-only-numbers-in-a-textbox-in-vb-net[^]
Use a Regex validator that .Net offers.
If you don't know what that is, use a search engine and get your "learn" on.
In the .net toolbox, is the control, NumericUpDown. It's so very handy.
You can set it to accept decimals of x length or integer.
Access it's value by, X as integer = NumUpDown.Value.
这篇关于使文本框仅接受数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文