Problems with textbox restriction input not working in WPF

Source: Internet
Author: User

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

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.