Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 78072

Highlighted Text does not scroll correctly

$
0
0

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.classSearchResult
02.{
03.    publicstringDisplay { get; set; }
04.    publicTextPointer Start { get; set; }
05.    publicTextPointer End { get; set; }
06.}
07. 
08.publicvoidDoSearch()
09.{
10.    stringtoSearch = 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 = newTextRange(RichTextBox_AllComments.Document.ContentStart, RichTextBox_AllComments.Document.ContentEnd);
25.    Regex reg = newRegex(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 inmatches)
35.            {
36.                textrange = newTextRange(start.GetPositionAtOffset(match.Index, LogicalDirection.Forward), start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward));
37.                SearchResults.Add(newSearchResult() { 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.privatevoidListBox_Find_SelectionChanged(objectsender, SelectionChangedEventArgs e)
46.{
47.    if(e.AddedItems.Count > 0)
48.    {
49.        bFindItemSelected = true;
50.        SearchResult sr = e.AddedItems[0] asSearchResult;
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.}

Viewing all articles
Browse latest Browse all 78072

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>