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
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
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.
Iva
the Telerik team
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)
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:
- 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
);
}));
}
- Invoke Measure on the imported document manually before proceeding:
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
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?
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 >>