C # common methods for inputting only numbers in Textbox (C #)

Source: Internet
Author: User
You can only enter numbers in Textbox (C #)

Private void tbox_keypress (Object sender, keypresseventargs E)
{
If (E. keychar = 0x20) E. keychar = (char) 0; // disable the Space key
If (E. keychar = 0x2d) & (textbox) sender). Text. Length = 0) return; // process negative numbers
If (E. keychar> 0x20)
{
Try
{
Double. parse (textbox) sender). Text + E. keychar. tostring ());
}
Catch
{
E. keychar = (char) 0; // handle invalid characters
}
}
}

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;
}

}

Private void textbox1_keypress (Object sender, system. Windows. Forms. keypresseventargs E)
{
If (E. keychar! = '\ B') // This indicates that the backspace key is allowed.
{
If (E. keychar <'0') | (E. keychar> '9') // This is a 0-9 number.
{
E. Handled = true;
}
}
}

Private void button#click (Object sender, eventargs E)
{
String text = This. textbox1.text;
If (text! = NULL)
MessageBox. Show (text );
}

Private void textbox1_validating (Object sender, canceleventargs E)
{
Const string pattern = @ "^ \ D + \.? \ D + $ ";
String content = (textbox) sender). text;

If (! (RegEx. ismatch (content, pattern )))
{
Errorprovider1.seterror (Control) sender, "only numbers can be entered! ");
E. Cancel = true;
}
Else
Errorprovider1.seterror (Control) sender, null );
}

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;
}

}

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; // remove unsuitable 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;
}
}
}

ASCII code,
{

If (E. keychar <= 48 | E. keychar> = 57) & (E. keychar! = 8) & (E. keychar! = 46 ))
E. Handled = true;
=======================48 represents 0, 57 represents 9, 8 represents space, 46 represents the decimal point
}

From: http://bbs.bccn.net/thread-205138-1-1.html

Related Article

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.