I'm trying to use a RichTextBox and my first feeling : "What's it's complicated to use !"... Amazing ...
So I'm trying to highlight a text contained in my RichTextBox.
I currently have the following code:
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
is correctly highlighted but not file
or end
but highlight me or
.
Any suggestions?
you have to imagine what the RichTextBox does under the hood to understand the behavior. I don't know exactly but I imagine the following: Line 1-2 set as Content of
RichTextBox
aParagraph
with aRun
.Then with the first iteration with
ApplyPropertyValue
the Content of the RichTextBox gets changed! It now contains aParagraph
with aSpan
(with aRun
inside) and a Run.And then you have to consider the discrepancy between the Regex match and
GetPositionAtOffset
. The Regex match returns an index for a char position in a string.GetPositionAtOffset uses "An offset, in symbols, for which to calculate and return the position" where a symbol is:
So what you might want to do is something like this:
*Disclaimer: I have not tried this as right now I'm nowhere near a development environment. I don't even know if this compiles, but I hope so.