Several common methods for entering numbers in a Winform textbox (C #)

Source: Internet
Author: User

Method one: private void Tbox_keypress (object sender, KeyPressEventArgs e) {if (E.keychar = = 0x20) E.keychar  = (char) 0; Disables the SPACEBAR if ((E.keychar = = 0x2D) && ((TextBox) sender).   Text.length = = 0)) return; Handle negative if (E.keychar > 0x20) {try {D Ouble. Parse ((TextBox) sender).                  Text + e.keychar.tostring ());   } catch {E.keychar = (char) 0;    Handle illegal Characters}}} method two: private void Textbox_keypress (object sender, KeyPressEventArgs e) {if (e.keychar!=8&&!      Char.isdigit (E.keychar)) {e.handled = true; }} or private void Textbox_keypress (object sender, KeyPressEventArgs e) {if (e.keychar!= ' \b ' &&!      Char.isdigit (E.keychar)) {e.handled = true; }} method Three: private void Textbox1_keypress (object sender, System.windowS.forms.keypresseventargs e) {if (e.keychar!= ' \b ')//This is the Allow input backspace key {if ((e.keychar< ' 0 ') | | (e.keychar> ' 9 '))  This is allowed to enter 0-9 digits {e.handled = true; }}} method four: private void Textbox1_validating (object sender, CancelEventArgs e) {const string pattern = @ "^\d+\ .?   \d+{1}quot;; String content = ((TextBox) sender).     Text; if (! (   Regex.IsMatch (content, pattern)) {errorProvider1.SetError (Control) sender, "can only enter numbers!");   E.cancel = true;   } else errorProvider1.SetError ((Control) sender, NULL);  } method Five: private void Textbox1_keypress (object sender, System.Windows.Forms.KeyPressEventArgs e) {if (e.keychar== '. ') && this.textBox1.Text.IndexOf (".")!  =-1) {e.handled=true; } if (! ( (e.keychar>=48 && e.keychar<=57) | | e.keychar== '. ' | |  e.keychar==8)) {e.handled=true; }} method Six: private void Tbx_lsregcapital_keypress (object sender, 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; }}} method seven: Using ASCII code processing, {if (E.keychar <= | | e.keych  Ar >=57) && (E.keychar! = 8) && (E.keychar =)) e.handled = true;  ================48 for 0, 57 for 9, 8 for spaces, and 46 for decimal 

Several common methods of entering numbers in a Winform textbox (C #)

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.