How do I make a textbox that only accepts numbers?

后端 未结 30 1638
梦如初夏
梦如初夏 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:40

    Just use a NumericUpDown control and set those ugly up down buttons visibility to false.

    numericUpDown1.Controls[0].Visible = false;
    

    NumericUpDown is actually a collection of controls containing a 'spin box' (up down buttons), a text box and some code to validate and wange-jangle it all together.

    Marking:

    YourNumericUpDown.Controls[0].visible = false 
    

    will hide the buttons while keeping the underlying code active.

    While not being an obvious solution it is simple and effective. .Controls[1] would hide the text box portion if you wanted to do that instead.

提交回复
热议问题