Reputation:
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
Upvotes: 1
Views: 588
Reputation: 126
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.
Upvotes: 1