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}