How do I make a textbox that only accepts numbers?

后端 未结 30 2147
梦如初夏
梦如初夏 2020-11-21 06:05

I have a windows forms app with a textbox control that I want to only accept integer values. In the past I\'ve done this kind of validation by overloading the KeyPress event

30条回答
  •  耶瑟儿~
    2020-11-21 06:49

    I have made something for this on CodePlex.

    It works by intercepting the TextChanged event. If the result is a good number it will be stored. If it is something wrong, the last good value will be restored. The source is a bit too large to publish here, but here is a link to the class that handles the core of this logic.

提交回复
热议问题