C # textbox limits input numbers

Source: Internet
Author: User

Private voidTbox_keypress (Objectsender, KeyPressEventArgs e) {            if(E.keychar = =0x20) E.keychar = (Char)0;//Disable SPACEBAR            if(E.keychar = =0x2D) && ((TextBox) sender). Text.length = =0))return;//Handling Negative numbers            if(E.keychar >0x20)            {                Try                {                    Double. Parse ((TextBox) sender). Text +e.keychar.tostring ()); }                Catch{E.keychar= (Char)0;//Handling Illegal characters                }            }        }Private voidTextbox_keypress (Objectsender, KeyPressEventArgs e) {    if(e.keychar!=8&&!char.isdigit (E.keychar)) {e.handled=true; }} or PrivatevoidTextbox_keypress (Objectsender, KeyPressEventArgs e) {    if(e.keychar!='\b'&&!char.isdigit (E.keychar)) {e.handled=true; }}Private voidTextbox1_keypress (Objectsender, System.Windows.Forms.KeyPressEventArgs e) {if(e.keychar!='\b')//This is allowed to enter the BACKSPACE bar{if((e.keychar<'0')|| (e.keychar>'9'))//This is allowed to enter 0-9 digits{e.handled=true;}}} Private voidButton1_Click (Objectsender, EventArgs e) { stringText = This. TextBox1.Text;if(Text! =NULL) MessageBox.Show (text); } Private voidTextbox1_validating (Objectsender, CancelEventArgs e) { Const stringPattern =@"^\d+\.? \d+$"; stringContent =((TextBox) sender). Text; if(!(Regex.IsMatch (content, pattern))) {errorprovider1.seterror (Control) sender,"only numbers can be entered!"); E.cancel=true; } Elseerrorprovider1.seterror (Control) sender,NULL); }Private voidTextbox1_keypress (Objectsender, System.Windows.Forms.KeyPressEventArgs e) {if(e.keychar=='.'&& This. TextBox1.Text.IndexOf (".")!=-1) {e.handled=true;}if(! ((e.keychar>= -&& e.keychar<= $) || e.keychar=='.'|| e.keychar==8) ) {e.handled=true;}} Private voidTbx_lsregcapital_keypress (Objectsender, KeyPressEventArgs e) {            if(! Char.isnumber (E.keychar) &&! Char.ispunctuation (E.keychar) &&!Char.iscontrol (E.keychar)) {e.handled=true;//Eliminate inappropriate characters            }            Else if(Char.ispunctuation (E.keychar)) {if(E.keychar! ='.'|| This. TextBox1.Text.Length = =0)//decimal point{e.handled=true; }                if(TextBox1.Text.LastIndexOf ('.') != -1) {e.handled=true; }}} using the ASCII code processing method, {if((E.keychar <= -|| E.keychar >= $) && (E.keychar! =8) && (E.keychar! = $)) e.handled=true;================48 for 0, 57 for 9, 8 for space, and 46 for decimal point}

C # textbox limits input numbers

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.