【发布时间】:2014-05-15 06:46:48
【问题描述】:
我尝试在 RichTextBox 中更改某些单词的颜色,最后我找到了一段代码,对我有很大帮助,但现在我遇到了问题。该代码仅对我的 RichTextBox 中的第一个“NULL”进行着色。
你能帮我找到解决办法吗?
感谢您的帮助!
private void searchWord()
{
String search = "NULL";
TextPointer text = RTBAuftrag.Document.ContentStart;
while (true)
{
TextPointer next = text.GetNextContextPosition(LogicalDirection.Forward);
if (next == null)
{
break;
}
TextRange txt = new TextRange(text, next);
int indx = txt.Text.IndexOf(search);
if (indx > 0)
{
TextPointer sta = text.GetPositionAtOffset(indx);
TextPointer end = text.GetPositionAtOffset(indx + search.Length);
TextRange textR = new TextRange(sta, end);
textR.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Red));
}
text = next;
}
}
有人知道这样做的可能方法吗?
【问题讨论】:
标签: c# colors richtextbox flowdocument words