编辑后保留文本的突出显示颜色 [英] Retain highlighted color of text after editing

查看:32
本文介绍了编辑后保留文本的突出显示颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除他前面一行的内容后,无法将我在 RichTextBox 中设置的突出显示效果保留在我的文本上.

Cannot keep the highlighted effect I set in my RichTextBox on my text after removing content of a line in front of him.

无论我从控件中删除多少文本,它总是会删除我设置为已包含在其中的文本的自定义 SelectionColor 和 SelectionBackColor.

No matter how much text I remove from the control it always removes the custom SelectionColor and SelectionBackColor I set to a text already contained in it.

我的移除方法代码:

private void btnRemove_Click(object sender, EventArgs e)
{
    //Remove selected line from RichTextBox
    richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.Text.Length - 1, 1);
    //Remove all blank lines remaining after deletion                  
    richTextBox1.Text = Regex.Replace(richTextBox1.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);
}

这里我要删除的字母数是 1,因为单词AND"是通过剪贴板粘贴方法插入的简单图像.

The number of letters I want to remove here is 1 as the word "AND" is a simple image inserted by means of Clipboard Paste method.

推荐答案

You must never (read my Lips: Never, never, never) 更改为 TextLines RichtTextBox 的属性,否则您将丢失/弄乱所有以前的格式.

You must never (read my lips: Never, never, never) change to Text or the Lines property of a RichtTextBox or else you will lose/mess up all previous formatting.

所以你需要改变这个:

richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.Text.Length - 1, 1);

对于这个序列:

首先选择Text中你想以某种方式改变的部分:

First Select the part of the Text you want to change in some way:

richTextBox1.SelectionStart = richTextBox1.Text.Length - 1;
richTextBox1.SelectionLength = 1;

现在您可以更改它.要删除任一使用:

Now you can change it. To delete either use:

richTextBox1.SelectedText = "";

richTextBox1.Cut(); 

后一个版本也会将文本放入剪贴板;这样做会保留该部分的格式,您可以粘贴到其他地方..

The latter version also will place the text in the clipboard; doing it it will keep the formatting of that portion and you could Paste it to some other place..

当您想要添加或更改任何类型的格式时,同样的规则适用:

The same rules apply when you want to add or change any type of formatting:

先选择再修改

而且,是的,这意味着第二个命令会增长很多,即您必须用循环替换 RegEx.Replace :-(

And, yes, this means that the second command will grow quite a bit, i.e. you will have to replace the RegEx.Replace by a loop :-(

这篇关于编辑后保留文本的突出显示颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆