问题:
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
来源:oschina
链接:https://my.oschina.net/u/4432649/blog/4422407