question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TextBox SelectAll Input Chinese crash

See original GitHub issue
  • .NET Core Version: .NET6
  • Windows version: (Windows10)
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

Problem description:TextBox SelectAll Input Chinese crash

    <Grid>
        <TextBox
            Name="textEdit"
            AcceptsReturn="False"
            TextWrapping="NoWrap" />
    </Grid>
      public MainWindow()
        {
            InitializeComponent();
            //In actual business, sometimes I need to assign a string with a newline character
            textEdit.Text = "testasdfasdfgasd阿斯顿发斯蒂芬\r\n撒旦法师打发斯蒂芬\r\n阿斯顿发送到发送到\r\n";
        }

When there are multiple lines of strings, when using the Chinese input method to input, the selected text will crash directly I know setting AcceptsReturn=“True” UndoLimit!=“0” will fix this But I need to use Enter event, so it doesn’t work for me So how should I solve this problem?

WPFTextBoxBug

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
miloushcommented, Nov 19, 2022

The documentation for AcceptsReturn says:

true if pressing the ENTER key inserts a new line at the current cursor position; otherwise, the ENTER key is ignored.

This is clearly wrong either way. First, it assumes that ENTER key will result into a new line in the text, which might not be the case for IME. Second, it also causes pasted text to be trimmed after the first new line, which normally has nothing to do with ENTER key. It does not prevent new lines in the Text property though, because you can set it directly. Changing either of these two behaviors would be a breaking change.

Filtering during composition update is problematic. First it is rude to mess with the IME by coercing its output in the middle of the composition. If the filtering is desired, it should happen when the composition is finished. Second, the IME is already done by this point, the framework is only replaying the composition events that had happened in the storage, so the IME cannot deal with the coercions even if it wanted to. And finally, it destabilizes the whole replay stack because the recorded text pointers will no longer be valid (you will run into that if you simply remove the assert) and fixing it up would be a lot of work that only the IME might be qualified to do.

I am not a big fan of all this filtering to begin with, but we can’t really remove it at this stage. I think the right thing to do here is to only do the filtering at the end of the composition, i.e. replace line 1981 above with text = composition.CompositionText.

(Note also that there is CharacterCasing property on the TextBox that is enforced by filtering. This uses the current input language and Unicode does not guarantee that such casing changes will not change the number of characters in the string, so this is a similar bug waiting to happen. Also note that the documentation for this property has a remark “This property does not affect characters that are added programmatically” which would be fitting for AcceptsReturn property too. And the AcceptsTab replaces tabs with spaces, which might have been a better design for AcceptsReturn too.)

1reaction
q812143836commented, Jan 20, 2023

@q812143836 - 由于上述解决方法的可用性,我们是否可以关闭它?

@q812143836 - Are we good to close this due to the availability of above workaround?

YES

Read more comments on GitHub >

github_iconTop Results From Across the Web

exception thrown from TextChanged event for chinese input
I am observing a crash while entering Text in Chinese language to TextBox with TextChanged event implemented. The text I am entering is...
Read more >
130867 – Changing Chinese font size in a text box would fail ...
Select all the characters, click menu "Format - Characters", and change the Asian Text Font size to 14. When clicking OK, Writer crashed....
Read more >
Sudden Crashes on Keyboard Input and File Download
In the case of typing, the crash report seems to put the issue inside of the ... When I switch among input methods,...
Read more >
WPF DataBinding sets Property twice for Chinese ...
I have the following code C# code: using System.ComponentModel; using System.Windows; namespace WpfApp1 { public partial class MainWindow ...
Read more >
TextBox.SelectAll() doesn't work - sometimes - C# / C Sharp
In a TextBox::KeyDown event I check to see if the Enter Key has been pressed, ... but the SelectAll() method appears to not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found