The recent use of the textbox to restrict input encountered a puzzling problem:
First look at the code:
<textbox name= "Txtip1" height= "All" width= "fontsize=" "3" maxlength= "textchanged=" >
private void Textbox_textchanged (object sender, Textchangedeventargs e)
{
try
{
//Shielding Chinese input and paste input
System.Windows.Controls.TextBox TextBox = sender As System.Windows.Controls.TextBox;
if (TextBox = = NULL ) return;
textchange[] Change = new Textchange[e.changes.count];
E.changes.copyto (change, 0);
int offset = change[0]. Offset;
if (Change[0]. Addedlength > 0)
{
Double num = 0;
if (! Double.tryparse (textbox.text, out num))
{
TextBox.Text = TextBox.Text.Remove (offset, change[0]. Addedlength);
Textbox.select (offset, 0);
}
}
if ( Comconvert.tryint (TextBox.Text) > 255)
{
TextBox.Text = TextBox.Text.Remove (textbox.text.length-1);
Textbox.selectionstart = TextBox.Text.Length;
}
}
catch (Exception ex)
{
Winmessage.show ("Input number is a problem, please re-enter!") ");
}
}
Feel the code is not a bit of a problem, can code run, found that the restrictions do not work, if the TextBox.Text = TextBox.Text.Remove (textbox.text.length-1); This sentence is commented out, although the MaxLength property is also effective, but can not limit the number is not greater than 255, and the Chinese input method is still possible to input. Originally thought in the textchanged incident to judge the length of the good, but the test found only effective once, the back will not work. Tried the KeyDown event again, and found the same. What do we do?
The workaround is simple: If you set the Inputmethod.isinputmethodenabled property to False in the TextBox, the property can also block the Chinese input method
Problems with textbox restriction input not working in WPF