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

Find Dialog vs. DocumentTextSearch.FindAll()

1 Answer 151 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mike
Top achievements
Rank 1
Mike asked on 28 Apr 2011, 08:57 PM
I have a fairly straightforward problem. I have some sample text in a RichTextBox that I'm searching for very simple tags in; i.e. <DTG/>.

My document's content is just text (no tables or anything fancy):
Serial: (X)<DTG/>
 
Content: This is<DTG/> some con<DTG/>tent, that I am <DTG/>testing out richtext find programmatically<DTG/>.

The "Find/Replace" dialog works flawlessly to find each instance of <DTG/>. Note that I am not using any special Regex, just the literal value. I use the dialog to search the document many times and it always finds 5 instances of <DTG/>.

(Edit)
If I highlight (via the ribbon bar) the 3rd and 4th instance of <DTG/>, the Find/Replace dialog returns erroneous results for my search.
(End Edit)

Now I want to do the exact same thing using DocumentTextSearch. It does work, the first time only. Subsequent calls give me partial, and then finally only 1 result. I am invoking the search on a button click with the same content as above in the document.

private void SearchAndHighlight( RadDocument doc, String search, RadRichTextBox rtb, Color highlight)
{
   doc.Selection.Clear();
 
   DocumentTextSearch search = new DocumentTextSearch( doc );
   foreach( var textRange in search.FindAll( search ) )
   {
      doc.Selection.AddSelectionStart( textRange.StartPosition );
      doc.Selection.AddSelectionEnd( textRange.EndPosition );
   }
    
   // set highlight
   rtb.ChangeTextHighlightColor( highlight );
 
   doc.Selection.Clear();
}

I do have a function that clears all highlighting before running the SearchAndHighlight code. I used the same code as presented in this forum to clear out highlighting.

If I comment out the highlight line, then the FindAll() works consistently each time I click my button and run this method.
// set highlight - this messes up subsequent calls to search.FindAll()!!!
// rtb.ChangeTextHighlightColor( highlight );

I really need a deterministic way to "Unhighlight", "Search", and finally "Highlight" from clicking a button.

1 Answer, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 29 Apr 2011, 03:04 PM
Apologies, the latest build of the tools works as intended.
Tags
RichTextBox
Asked by
Mike
Top achievements
Rank 1
Answers by
Mike
Top achievements
Rank 1
Share this question
or