In the text box (TextBox), only numbers and decimal places are allowed. -- (reference: column of zyg19800719)

Source: Internet
Author: User
// KeyPress event: when the control gets the focus and the user presses and releases the key on the keyboard
Private void textBox1_KeyPress (object sender, KeyPressEventArgs e) // the text box only accepts numbers and decimal places
{
// IsNumber: Specifies whether the character at the specified position in the string belongs to the digit category.
// IsPunctuation: Specifies whether the character at the specified position in the string belongs to the punctuation category.
// IsControl: Specifies whether the character at the specified position in the string belongs to the control character category.
If (! Char. IsNumber (e. KeyChar )&&! Char. IsPunctuation (e. KeyChar )&&! Char. IsControl (e. KeyChar ))
{
E. Handled = true; // gets or sets a value to indicate whether the System. Windows. Forms. Control. KeyPress event has been processed.
}
Else if (Char. IsPunctuation (e. KeyChar ))
{
If (e. KeyChar = '.')
{
If (TextBox) sender). Text. LastIndexOf ('.')! =-1)
{
E. Handled = true;
}
}
Else
{
E. Handled = true;
}
}
}

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.