简体   繁体   English

c# WPF 删除当前的富文本框行

[英]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 RichTextBox 使用换行符(或内联)代替分段符

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.由于这不适用于最后一行,因此您需要有一个 if,它使用 DocumentEnd 的属性检查它是否为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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