In C #, you can only enter a number and set the upper limit in a TextBox control

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.