First, you can enter only numbers:
First, set the KeyPress event for the TextBox control: When the user presses the key of the keyboard is not in the digital bit, it is forbidden to enter
1 Private void textbox1_keypress (object sender, KeyPressEventArgs e)2 { 3 if8 &&! Char.isdigit (E.keychar))// If you do not enter a number, do not let input 4 {5 true ; 6 }7 }
Set the upper limit:
Set the TextChanged event for the textbox as follows
1 Private voidtextBox1_TextChanged (Objectsender, EventArgs e)2 {3 intIMax = -;//first set the upper value4 if(TextBox1.Text! =NULL&& TextBox1.Text! ="")//determines that the contents of the textbox are not NULL, and if not judged will result in subsequent non-numeric contrast anomalies5 {6 if(int. Parse (TextBox1.Text) > IMax)//num is the value passed in, if it is greater than the upper limit (the input value), then it is forced to 1, or the upper limit value? 7 {8TextBox1.Text = (IMax-1). ToString ();9 }Ten } One}
In C #, you can only enter a number and set the upper limit in a TextBox control