c# – Undo the transfer of RichEditBox

Question:

When I press Enter, a message is sent to me and the field is cleared

private void Clear_Enter(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                string str;
                Message_BOX.Document.GetText(Windows.UI.Text.TextGetOptions.None, out str);
                while (str[str.Length - 1] == '\n' || str[str.Length - 1] == '\r')
                    str = str.Substring(0, str.Length - 1);
                Message_BOX.Document.SetText(Windows.UI.Text.TextSetOptions.None, str);
            }
        }

But before clearing, there is still a line break and the wrap animation is visible. How to remove hyphenation?

Answer:

In my opinion, the simplest and surest way to achieve the desired effect is to create an OnKeyDown RichEditBox and override the OnKeyDown method. This is how it might look:

public class CustomRichTextBox : RichEditBox
{
    protected override void OnKeyDown(KeyRoutedEventArgs e)
    {
        // invoked anytime a key is pressed down, independent of focus
        if(e.Key == VirtualKey.Enter)
        {
            Document.SetText(Windows.UI.Text.TextSetOptions.None, "");
            return;
        }
        base.OnKeyDown(e);
    }
}

We do not pass Enter to the RichEditBox handler, so there will be no processing of pressing this key, therefore, you will not see any animation and other things.

Scroll to Top