I have formatted text in the rtf file and I load it to my richTextBox. It works fine, but then I want to remove some text parts programmatically. If I do like that:

richTextBox.LoadFile("TextFile.rtf");
richTextBox.Text = richTextBox1.Text.Substring(fromPosition, length);

text formatting disappears. I've tried to work with richTextBox.Lines, but there is no delete or edit function. All I need is to delete parts of loaded text without losing its formatting. Is it possible?

Thanks in advance.

有帮助吗?

解决方案

To remove text you first select it, programmatically by setting SelectionStart and -Length. Then you use the Cut Method:

    richTextBox1.SelectionStart = 20;
    richTextBox1.SelectionLength = 120;
    richTextBox1.Cut();

If you want to avoid putting the removed text to the clipboard you can set it to "" instead of 'cutting' it:

    richTextBox1.SelectedText = ""
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top