This is a migrated thread and some comments may be shown as answers.

Highlighted Text does not scroll correctly

0 Answers 69 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
webmaster
Top achievements
Rank 1
webmaster asked on 09 Sep 2016, 04:41 PM

I have a RichTextBox that I am searching.  When the search result is selected the text is then highlighted correctly.  However, once the RichTextBox is scrolled, the highlight does not follow the text that was highlighted, but instead stays where the text was initially found (see attached screenshot). Below is the code that highlights the desired text initially.  Any thoughts on how to get the highlight to follow the text as it is scrolled?

01.class SearchResult
02.{
03.    public string Display { get; set; }
04.    public TextPointer Start { get; set; }
05.    public TextPointer End { get; set; }
06.}
07. 
08.public void DoSearch()
09.{
10.    string toSearch = TextBox_Find.Text.Replace(@"\", @"\\")
11.        .Replace("^", @"\^")
12.        .Replace("$", @"\$")
13.        .Replace(".", @"\.")
14.        .Replace("|", @"\|")
15.        .Replace("?", @"\?")
16.        .Replace("*", @"\*")
17.        .Replace("+", @"\+")
18.        .Replace("(", @"\(")
19.        .Replace(")", @"\)")
20.        .Replace("[", @"\[")
21.        .Replace("]", @"\]")
22.        .Replace("{", @"\{")
23.        .Replace("}", @"\}");
24.    TextRange range = new TextRange(RichTextBox_AllComments.Document.ContentStart, RichTextBox_AllComments.Document.ContentEnd);
25.    Regex reg = new Regex(toSearch, RegexOptions.Compiled | RegexOptions.IgnoreCase);
26. 
27.    var start = RichTextBox_AllComments.Document.ContentStart;
28.    while (start != null && start.CompareTo(RichTextBox_AllComments.Document.ContentEnd) < 0)
29.    {
30.        if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
31.        {
32.            var matches = reg.Matches(start.GetTextInRun(LogicalDirection.Forward));
33.            TextRange textrange = null;
34.            foreach (Match match in matches)
35.            {
36.                textrange = new TextRange(start.GetPositionAtOffset(match.Index, LogicalDirection.Forward), start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward));
37.                SearchResults.Add(new SearchResult() { Display = "Result " + (SearchResults.Count + 1).ToString(), Start = textrange.Start, End = textrange.End });
38.            }
39.            if (textrange != null) start = textrange.End;
40.        }
41.        start = start.GetNextContextPosition(LogicalDirection.Forward);
42.    }
43.}
44. 
45.private void ListBox_Find_SelectionChanged(object sender, SelectionChangedEventArgs e)
46.{
47.    if (e.AddedItems.Count > 0)
48.    {
49.        bFindItemSelected = true;
50.        SearchResult sr = e.AddedItems[0] as SearchResult;
51.        RichTextBox_AllComments.Focus();
52.        RichTextBox_AllComments.Selection.Select(sr.Start, sr.End);
53.        ((UIElement)ListBox_Find.ItemContainerGenerator.ContainerFromItem(ListBox_Find.SelectedItem)).Focus();
54.    }
55.}

No answers yet. Maybe you can help?

Tags
RichTextBox
Asked by
webmaster
Top achievements
Rank 1
Share this question
or