我想使用RichTextBox,但我的第一感觉是:“它的使用太复杂了!”... 真是令人惊讶...
所以我想要在我的RichTextBox中突出显示一段文本。
我目前有以下代码:
TextRange range = new TextRange(MyTextInput.Document.ContentStart, MyTextInput.Document.ContentEnd);
range.Text = @"TOP a multiline text or file END";
Regex reg = new Regex("(top|file|end)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match match in reg.Matches(range.Text))
{
TextPointer start = range.Start.GetPositionAtOffset(match.Index, LogicalDirection.Forward);
TextPointer end = range.Start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward);
// text contains the exact match I want
string text = range.Text.Substring(match.Index, match.Length);
// here the highlighted text isn't the text I searched...
TextRange textrange = new TextRange(start, end);
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
textrange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
}
TOP
被正确地突出显示,但是 file
或者 end
没有被突出显示,但是突出显示了我 or
。
有什么建议吗?
var
让我困惑的典型例子。因为我不熟悉这些属性,也不知道例如ContentStart
是索引(可能是)还是字符或其他什么。 - Caster Troy