Crashing When textbox text updating on TextChanged Event for Non English Keyboard

JABIR E E 1 Reputation point
2020-08-19T05:35:12.093+00:00

Hi Team,

When Typing something on the TextBox which has following code snippet on Text Changed that causes a crashing when keyboard is non English (Japanses/Chinese)

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
textBox.TextChanged -= TextBox_TextChanged;
if(!double.TryParse(textBox.Text,out double result))
{
textBox.Text = "8";
}
textBox.TextChanged += TextBox_TextChanged;

    }

Please let me know why this is happening.

Sincerely,
Jabir E

Developer technologies Windows Presentation Foundation
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,646 Reputation points
    2020-08-19T08:47:31.687+00:00

    I test you code and the app crashed with the error:The error code is 0x80131623. This error may be a bug in the CLR or in the unsafe..... The reason for this error is that a large number of TextBox_TextChanged events were triggered which cause memory issue. You can try the below code and input as you did in the question said.

     private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                //add a breakpoint here.
            }
    

    You will meet the same error, so you need to be careful to use TextBox_TextChanged event. If you want to do limit the TextBox for typing, you can use a DependencyProperty as the backing store for Input Limit. Looking like this:

    public static readonly DependencyProperty InputLimitProperty =
                DependencyProperty.RegisterAttached("InputLimit", typeof(bool), typeof(TextBoxExtension), new PropertyMetadata(false, InputLimitChanged));
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.