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

StackOverflowException when inserting section break after a paragraph

2 Answers 66 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Jack
Top achievements
Rank 1
Jack asked on 09 Dec 2012, 11:25 AM
I keep getting this error when inserting a section break after a paragraph.I tagged the document element where i wanted to add section break.


 var taggedElements = documentElements.Where(e => e.Tag == "SectionBreak");

                IList<DocumentPosition> breaks = new List<DocumentPosition>();

                if (taggedElements.Count() > 0)
                {
                    foreach (DocumentElement element in taggedElements)
                    {
                        var inlineBox = element.FirstLayoutBox as ParagraphLayoutBox;
                        InlineLayoutBox inline = inlineBox.ChildLayoutBoxes.First as InlineLayoutBox;
                        MyRadDocument.CaretPosition.MoveToInline(inline,0);
                        MyRadDocument.CaretPosition.MoveToLastPositionInParagraph();
                        breaks.Add(MyRadDocument.CaretPosition);
                    }
                }


                foreach (DocumentPosition pos in breaks)
                {
                    MyRadDocument.InsertSectionBreak(pos, MyRadDocument.Style, Telerik.Windows.Documents.SectionBreakType.NextPage);
                }

Any ideas?

2 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 12 Dec 2012, 05:50 PM
Hello Jack,

I tested this case and was unable to reproduce a StackOverflowException. However, I observe some inconsistent behavior on our end when more than one tagged element is present in the document.

When you add the elements in your breaks collection all positions are as should be. However, once you perform the first insertion of a section break, all sequential positions point to the wrong place, which is expected. 

Also, though the method you choose for inserting a section break works, the Undo command cannot be used to reverse each inserted break. In order to preserve the history, you should use a method of the editor instead.

I have modified the above code to use the editor method and avoided traversing the layout boxes of the document in the process as that too may cause issues. In addition, I was able to implement the method with one foreach cycle, so it should also perform better.
RadDocument MyRadDocument = this.editor.Document;
 
var documentElements = MyRadDocument.EnumerateChildrenOfType<Paragraph>();
var taggedElements = documentElements.Where(e => e.Tag == "SectionBreak").ToList();
 
foreach (Paragraph paragraph in taggedElements)
{
    MyRadDocument.CaretPosition.MoveToInline(paragraph.Inlines.First);
    MyRadDocument.CaretPosition.MoveToLastPositionInParagraph();
 
    editor.InsertSectionBreak(SectionBreakType.NextPage);
}


I hope this will be helpful. Let us know how it goes and if you are still experiencing issues get back to us with additional details (stack trace, etc.).

 
All the best,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Jack
Top achievements
Rank 1
answered on 16 Dec 2012, 11:30 PM
Thanks much.

Was able to use this approach..
Tags
RichTextBox
Asked by
Jack
Top achievements
Rank 1
Answers by
Petya
Telerik team
Jack
Top achievements
Rank 1
Share this question
or