简体   繁体   中英

c# WPF Remove current richtextbox line

I've set up an event handler that captures when the backspace key is pressed for a richtextbox. It works great to delete a character, but it stops working when you get to the end of the line. Now I need it to delete the entire line when the line is empty and the key is pressed and set the caret position to the end of the previous line. Just like a regular text editor. PS. The RichTextBox uses line breaks (or inlines) instead of paragraph breaks

var TextBox = Instance.RichTextBox;

            if (TextBox.CaretPosition.IsAtLineStartPosition)
            {
                //Remove the line and set the caret position to the end of the previous line
            }
            else
            {
                TextBox.CaretPosition.DeleteTextInRun(-1);
            }

            e.Handled = true;

I've got no idea how to make this work any help is appreciated and thanks in advance

To get the end of a line, you can get the start of next line and then go back to prior insertion position.

This is how you can do that:

txtPointer.GetLineStartPosition(1).GetInsertionPosition(LogicalDirection.Backward);

Since this won't work for the last line, you need to have an if, which checks if it is null use the property of DocumentEnd.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM