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

Online example of FindReplace has a bug

6 Answers 91 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Grayson Mitchell
Top achievements
Rank 1
Grayson Mitchell asked on 26 Feb 2011, 10:16 PM
Using the online help I found an example how to code find and replace.  Unfortunately the code (which I have not modified) causes a stackoverflowexception.

This is the line of code that causes the error (I am just searching for "XXX")
            foreach (var textRange in search.FindAll(toSearch))

The whole method:
private void ReplaceAllMatches(string toSearch, string toReplaceWith)
        {
            this.radRichTextBox1.Document.Selection.Clear(); // this clears the selection before processing
            DocumentTextSearch search = new DocumentTextSearch(this.radRichTextBox1.Document);
            List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
            foreach (var textRange in search.FindAll(toSearch))
            {
                TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
                rangesTrackingDocumentChanges.Add(newRange);
            }
            foreach (var textRange in rangesTrackingDocumentChanges)
            {
                this.radRichTextBox1.Document.Selection.AddSelectionStart(textRange.StartPosition);
                this.radRichTextBox1.Document.Selection.AddSelectionEnd(textRange.EndPosition);
                this.radRichTextBox1.Insert(toReplaceWith);
                textRange.StartPosition.Dispose();
                textRange.EndPosition.Dispose();
            }
        }

6 Answers, 1 is accepted

Sort by
0
Grayson Mitchell
Top achievements
Rank 1
answered on 27 Feb 2011, 12:08 AM
After a coffee, I discovered that the provider.Import(file.FileContent) command is asynchronous, and the error is occurring due to the document not being created at this point.

So I have now got the find and replace working (from a button press), however I can't find an event that will tell me when the Import is complete! (I want to do the find/replace as soon as the document has loaded)

Cheers
Grayson
0
Iva Toteva
Telerik team
answered on 01 Mar 2011, 06:02 PM
Hi Grayson,

It is good to hear that you have found the explanation of the strange behavior you were observing.
As for your new question, have you tried the DocumentChanged event of RadRichTextBox? It sounds like it is just what you need.
If that does not answer your questions or you need further assistance, do not hesitate to contact us again.

Best wishes,
Iva
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Grayson Mitchell
Top achievements
Rank 1
answered on 04 Mar 2011, 11:03 PM
No the DocumentChanged event does not work in this case as it fires the event on:
  this.radRichTextBox1.Document = provider.Import(file.FileContent);

But when it reaches the following: 
 
this.radRichTextBox1.Document.Selection.Clear(); // this clears the selection before processing
DocumentTextSearch search = new DocumentTextSearch(this.radRichTextBox1.Document);

foreach (var textRange in search.FindAll(toSearch))
 

I get a stackoverflow... I am guessing this is due to the RichTextBox is no fully Loaded at this point (as I can run the same code from a button click and it works)
0
Iva Toteva
Telerik team
answered on 09 Mar 2011, 12:09 PM
Hi Grayson,

You are right, replacing the words at document changed does not work either, because the document that is created is still not measured when the event is fired. This leaves you to the following options:
  1. Use Dispatcher to assure that the document is measured before invoking the replace function like this:
    Copy Code
    private void radRichTextBox_DocumentChanged(object sender, EventArgs e)
    {
        Dispatcher.BeginInvoke(new Action(() =>
        {
            ReplaceAllMatches(this.radRichTextBox.Document, toSearch, toReplaceWith);
        }));
    }
  2. Invoke Measure on the imported document manually before proceeding:
Copy Code
RadDocument document = LoadXamlFile();
document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
ReplaceAllMatches(document,  toSearch, toReplaceWith);
this.radRichTextBox.Document = document;

If these solutions do not work, we would greatly appreciate a sample project illustrating the problem you are observing.

Kind regards,
Iva
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Manuel
Top achievements
Rank 1
answered on 28 Dec 2011, 04:27 PM
Hi Iva,

I tried your code, but the result was bad.  First the method don´t find well because didn´t replace in thw right place, and repeated and write the word that I want replace 90 times!!

Here is the code:

 

private void ReplaceAllMatches(string toSearch, string toReplaceWith,RadDocument doc)
        {
          
            doc.Selection.Clear();
            
            DocumentTextSearch search = new DocumentTextSearch(doc);
            List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
          
            foreach (var textRange in search.FindAll(toSearch))
            {
                TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
                rangesTrackingDocumentChanges.Add(newRange);
            }

            foreach (var textRange in rangesTrackingDocumentChanges)
            {
                doc.Selection.AddSelectionStart(textRange.StartPosition);
                doc.Selection.AddSelectionEnd(textRange.EndPosition);
                doc.Insert(toReplaceWith,null);
                textRange.StartPosition.Dispose();
                textRange.EndPosition.Dispose();
            }
        }

Any idea because repeat 90 times?

0
Boby
Telerik team
answered on 30 Dec 2011, 11:05 AM
Hello Manuel,
It looks you've found the answer in other thread you opened. Don't hesitate to contact us if you have other questions.

Greetings,
Boby
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Grayson Mitchell
Top achievements
Rank 1
Answers by
Grayson Mitchell
Top achievements
Rank 1
Iva Toteva
Telerik team
Manuel
Top achievements
Rank 1
Boby
Telerik team
Share this question
or