Note that there are some explanatory texts on larger screens.

plurals
  1. PORichTextBox search'n'replace results are staggered
    primarykey
    data
    text
    <p>I am currently trying to generate colored results after a search containing keywords. My code displays a richtextbox containing a text that was succesfully hit by the search engine.</p> <p>Now I want to highlight the keywords in the text, by making them bold and colored in red. I have my list of words in a nice string table, which I browse this way (rtb is my RichTextBox, plainText is the only Run from rtb, containing the entire text of it) :</p> <pre><code>rtb.SelectAll(); string allText = rtb.Selection.Text; string expression = ""; foreach (string word in words) { expression = Regex.Escape(word); Regex regExp = new Regex(expression); foreach (Match match in regExp.Matches(allText)) { TextPointer start = plainText.ContentStart.GetPositionAtOffset(match.Index, LogicalDirection.Forward); TextPointer end = plainText.ContentStart.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Forward); rtb.Selection.Select(start, end); rtb.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold); rtb.Selection.ApplyPropertyValue(Run.ForegroundProperty, "red"); } } </code></pre> <p>Now I thought this would do the trick. But somehow, only the first word gets highlighted correctly. Then, the second occurence of the highlights starts two early, with the correct amount of letters getting highlighted, but a few characters before the actual word. Then for the third occurence it's further more characters earlier, etc.</p> <p>Have you got any idea what is causing this behavior?</p> <p>EDIT (01/07/2013): Still not figuring out why these results are staggered... So far I noticed that if I created a variable set to zero right before the second foreach statement, added it up to each textpointer's positions and incremented it by 4 (no idea why) at the end of each loop, the results are colored adequately. Nevertheless, if I search for two keywords or more (doesn't matter if they're the same size), each occurence of the first keyword get colored correctly, but only the first occurences of the other keywords are well-colored. (the others are staggered again) Here's the edited code:</p> <pre><code>rtb.SelectAll(); string allText = rtb.Selection.Text; string expression = ""; foreach (string word in words) { expression = Regex.Escape(word); Regex regExp = new Regex(expression); int i = 0; foreach (Match match in regExp.Matches(allText)) { TextPointer start = plainText.ContentStart.GetPositionAtOffset(match.Index + i, LogicalDirection.Forward); TextPointer end = plainText.ContentStart.GetPositionAtOffset(match.Index + match.Length + i, LogicalDirection.Forward); rtb.Selection.Select(start, end); rtb.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold); rtb.Selection.ApplyPropertyValue(Run.ForegroundProperty, "red"); i += 4; // number found out from trials } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload