Online example of FindReplace has a bug

7 posts, 0 answers
  1. Grayson Mitchell
    Grayson Mitchell avatar
    12 posts
    Member since:
    Jun 2009

    Posted 26 Feb 2011 Link to this post

    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();
                }
            }
  2. Grayson Mitchell
    Grayson Mitchell avatar
    12 posts
    Member since:
    Jun 2009

    Posted 26 Feb 2011 Link to this post

    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
  3. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 01 Mar 2011 Link to this post

    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!
  4. Grayson Mitchell
    Grayson Mitchell avatar
    12 posts
    Member since:
    Jun 2009

    Posted 04 Mar 2011 Link to this post

    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)
  5. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 09 Mar 2011 Link to this post

    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!
  6. Manuel
    Manuel avatar
    61 posts
    Member since:
    Oct 2011

    Posted 28 Dec 2011 Link to this post

    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?

  7. Boby
    Admin
    Boby avatar
    685 posts

    Posted 30 Dec 2011 Link to this post

    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 >>

Back to Top