C # text Box entry restrictions (1): Digital input

Source: Internet
Author: User
Tags numeric
text box

In many cases, we need to limit the contents of the input text box to avoid the entry of spam and illegal information. But many of the methods listed in the textbook are more or less flawed, or they cannot prevent Chinese from being imported or effectively masking the Chinese content in the Clipboard. Today, we introduce a method that allows text boxes to enter only numbers (0-9) and prevents illegal pasting and Chinese input.

Here to handle the TextChanged event, prevent the text box from accepting non-numeric content:

The public void Mybox_textchanged (object sender, System.EventArgs e)
89.. {
The string txt = mybox.text;
The ' int i = txt '. Length;
if (I < 1) return;
A for (int m = 0; m < i; m + +)
94.. {
String str = txt. Substring (M, 1);
The IF (!char). Isnumber (Convert.tochar (str))
97.. {
Mybox.text = MyBox.Text.Replace (str, ""); Filter out non-numeric text
Mybox.selectionstart = mybox.text.length;//Position the cursor to the last
100}
101}
102}




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.