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

How I can replace a certain text with a table

2 Answers 20 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Harald
Top achievements
Rank 1
Harald asked on 12 Apr 2015, 04:05 PM

I would replace the text %Doctors% at the runtime by a table. The following code does not work.

private void ReplaceAllMatches(string Parameter, Telerik.Windows.Documents.Model.Table NewTable)
{
this.myViewer.Document.Selection.Clear(); // this clears the selection before processing
DocumentTextSearch search = new DocumentTextSearch(this.myViewer.Document);
List<Telerik.Windows.Documents.TextSearch.TextRange> rangesTrackingDocumentChanges = new List<Telerik.Windows.Documents.TextSearch.TextRange>();
foreach (var textRange in search.FindAll(Parameter))
{
Telerik.Windows.Documents.TextSearch.TextRange newRange = new Telerik.Windows.Documents.TextSearch.TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
rangesTrackingDocumentChanges.Add(newRange);
}
foreach (var textRange in rangesTrackingDocumentChanges)
{
this.myViewer.Document.Selection.AddSelectionStart(textRange.StartPosition);
this.myViewer.Document.Selection.AddSelectionEnd(textRange.EndPosition);
this.myViewer.InsertTable(NewTable,true);
textRange.StartPosition.Dispose();
textRange.EndPosition.Dispose();
}
}

 

2 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 15 Apr 2015, 04:52 PM
Hi Harald,

I believe the observed behavior might be related to the fact that the Table elements are passed by reference and you are trying to insert the same table in more than one position. Could you verify this is the case? If yes, I suggest passing different Table objects to the InsertTable() method.

Hope this helps.

Regards,
Tanya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Harald
Top achievements
Rank 1
answered on 15 Apr 2015, 08:26 PM

Yes, I would like to insert the same table in different positions. After I modified the code, it works.

...

foreach (var textRange in rangesTrackingDocumentChanges)
{
this.myViewer.Document.Selection.AddSelectionStart(textRange.StartPosition);
this.myViewer.Document.Selection.AddSelectionEnd(textRange.EndPosition);
this.myViewer.Delete(true);
this.myViewer.InsertTable(NewTable,true);
textRange.StartPosition.Dispose();
textRange.EndPosition.Dispose();
}

...

Tags
RichTextBox
Asked by
Harald
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Harald
Top achievements
Rank 1
Share this question
or