如何制作仅接受数字的文本框?

筅森魡賤 提交于 2020-10-25 07:39:55

问题:

I have a windows forms app with a textbox control that I want to only accept integer values. 我有一个带有文本框控件的Windows窗体应用程序,我只想接受整数值。 In the past I've done this kind of validation by overloading the KeyPress event and just removing characters which didn't fit the specification. 过去,我通过重载KeyPress事件并仅删除不符合规范的字符来进行这种验证。 I've looked at the MaskedTextBox control but I'd like a more general solution that could work with perhaps a regular expression, or depend on the values of other controls. 我已经看过MaskedTextBox控件,但是我想要一个更通用的解决方案,它可以使用正则表达式,也可以依赖于其他控件的值。

Ideally this would behave such that pressing a non numeric character would either produce no result or immediately provide the user with feedback about the invalid character. 理想情况下,这样做是为了使按下非数字字符不会产生任何结果,或者会立即向用户提供有关无效字符的反馈。


解决方案:

参考一: https://stackoom.com/question/1wWZ/如何制作仅接受数字的文本框
参考二: https://oldbug.net/q/1wWZ/How-do-I-make-a-textbox-that-only-accepts-numbers
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!