富文本框如何突出显示文本块而不选择

时间:2012-06-25 08:54:21

标签: c# .net winforms richtextbox

我需要在RTB中突出显示我文本的某一部分,而不是改变字体样式/颜色,而是使用特定颜色进行块选择。这类似于Visual Studio在调试模式下突出显示一行的方式。

这里重要的是使用richtextbox.select函数实现上述功能 WITHOUT ,因为我所拥有的richtextbox正在定期更新,如果它在每次更新时调用select函数,则用户将拖动文本很困难,我不希望这种情况发生。

我在编辑rtf的某个地方听到了一个解决方案,但我并不完全确定如何做到这一点。我很感激任何帮助。

2 个答案:

答案 0 :(得分:4)

编辑:刚刚意识到问题是winforms。以下答案适用于WPF,而不是删除我会离开它,万一有人发现它有用。

使用TextRange(...)获取文本并应用背景 例如:

TextRange tr = new TextRange(position, nextPosition);
var Br = new SolidColorBrush(Color.FromScRgb(alpha, 1f, 1f, 0f));
tr.ApplyPropertyValue(TextElement.BackgroundProperty, Br);

但也许您应该研究一下您的更新机制并提出更好的解决方案。

答案 1 :(得分:2)

这不使用richtextbox的Select()函数。它只是在start and end index坐标and updates the region中使用选择with的{​​{1}}。

colour
将在 // change the co-ordinates as per the selection in the run-time richTextBox1.Text = "Select some text"; richTextBox1.SelectionStart = 0; richTextBox1.SelectionLength = 4; richTextBox1.SelectionBackColor = Color.LightBlue; 中为上述代码选择

Sele

相关问题