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

inline does not belong to this document

2 Answers 200 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 29 Dec 2012, 08:50 PM

Is there any reason when executing either of the following two commands I should get the following error?

Command Executing:
//Loop Through Document Sections
foreach (Section sectionLayoutBox in radRichTextBox.Document.Children)
{
    //Loop Through Section Paragraphs
    foreach (Paragraph paragraph in sectionLayoutBox.EnumerateChildrenOfType<Paragraph>().ToList<Paragraph>())
    {
        //Remove Paragraph From Document
        //paragraph.Parent.Children.Remove(paragraph);
        sectionLayoutBox.Children.Remove(paragraph);
    }
}


Error Message: inline does not belong to this document.

I know my initial post is a little vague, I can elaborate in more detail if you require.

Thanks,

Rob

2 Answers, 1 is accepted

Sort by
0
Robert
Top achievements
Rank 1
answered on 29 Dec 2012, 08:52 PM
Here's more code if it helps:

//Room to change found flag
bool roomToChangeFound = false;
 
//List of Document Fragments (paragraphs after room title)
List<DocumentFragment> paragraphsAfterRoom = new List<DocumentFragment>();
 
 
//Loop Through Document Sections
foreach (Section sectionLayoutBox in radRichTextBox.Document.Children)
{
    //Loop Through Section Paragraphs
    foreach (Paragraph paragraph in sectionLayoutBox.EnumerateChildrenOfType<Paragraph>().ToList<Paragraph>())
    {
        //Loop Through Paragraph AnnotationRanges
        foreach (AnnotationRangeMarkerBase annotationMarker in paragraph.EnumerateChildrenOfType<AnnotationRangeMarkerBase>().ToList<AnnotationRangeMarkerBase>())
        {
            //Check if Room Annotation has been found in Paragraph
            if (annotationMarker is RoomGroupRangeStart)
            {
                if (annotationMarker == oldRoom)
                {
                    //Flag Room To Remove Has Been Found
                    roomToChangeFound = true;
                    break;
                }
                else
                {
                    roomToChangeFound = false;
                    break;
                }
            }
        }
 
        //Create List of Paragraphs To Move Following Room Title
        if (roomToChangeFound == true)
        {
            //Clear Document Selection
            radRichTextBox.Document.Selection.Clear();
 
            //Get Paragraph start & end positions
            DocumentPosition startPosition = new DocumentPosition(radRichTextBox.Document.CaretPosition);
            startPosition.MoveToInline(paragraph.Inlines.First);
 
            DocumentPosition endPosition = new DocumentPosition(startPosition);
            endPosition.MoveToLastPositionInParagraph();
 
            //Select Paragraph
            radRichTextBox.Document.Selection.SetSelectionStart(startPosition);
            radRichTextBox.Document.Selection.AddSelectionEnd(endPosition);
 
            //Store Paragraph In List<>
            DocumentFragment fragment = radRichTextBox.Document.Selection.CopySelectedDocumentElements();
            paragraphsAfterRoom.Add(fragment);
 
            //Clear Selection
            radRichTextBox.Document.Selection.Clear();
 
            //Remove Paragraph From Document
            //paragraph.Parent.Children.Remove(paragraph);
            sectionLayoutBox.Children.Remove(paragraph);
        }
        else if (roomToChangeFound == false && paragraphsAfterRoom.Count > 0)
        {
            break;
        }
    }
}
0
Petya
Telerik team
answered on 02 Jan 2013, 11:57 AM
Hi Rob,

A possible reason for such a behavior is the method you choose for deleting the desired paragraphs. Overall, you should not add or remove document elements directly into the structure of a measured document (document shown in the editor), as that may cause document invalidation and lead to errors such as the one observed.

Instead, you should take advantage of the Delete method of RadRichTextBox which works over the current selection. Respectively, the InsertFragment method can be used for inserting document elements at the caret location. In your case the deletion can happen prior to clearing the selection.

If this does not help we would appreciate if you open a support ticket and send us a project we can use for testing.
 
Kind regards,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
RichTextBox
Asked by
Robert
Top achievements
Rank 1
Answers by
Robert
Top achievements
Rank 1
Petya
Telerik team
Share this question
or