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

Section.Blocks.Remove(delParagraph) crashes

2 Answers 211 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Evaldas asked on 10 Jul 2014, 08:21 AM
I have a hard time finding a sollution to delete paragraph from open document. Code below crashes on aither delSection.Blocks.Remove(delParagraph); or later on radRichTextBox.UpdateEditorLayout(); after remove vas called. The error is: "inline does not belong to this document.". This seems to happen randomly. Sometimes everything passes without an error. I cant fine a way to get this working on regular basis.

01.private void DeleteTopicFromDoc(string tag)
03.    Section delSection = null;
04.    Block delParagraph = null;
06.    foreach (Section section in radRichTextBox.Document.Sections)
07.    {
08.        foreach (var block in section.Blocks)
09.        {
10.            var paragraph = block as Paragraph;
11.            if (paragraph != null)
12.            {
13.                string topicTag = string.Format("topic_{0}", tag);
14.                if (paragraph.Tag == topicTag)
15.                {
16.                    delSection = section;
17.                    delParagraph = block;
18.                    break;
19.                }
20.            }
21.        }
22.        if (delParagraph != null) break;
23.    }
25.    if (delParagraph != null)
26.    {
27.        delSection.Blocks.Remove(delParagraph);
28.    }

Also tried a different approach found on this forum, that was suggested by admin. It was that I use selection to select paragraph to remove and the call 
radRichTextBox.Delete(false); witch deletes selected content. But the problem is that I have to remove a ReadOnlyRange. So this approach does not fit.

2 Answers, 1 is accepted

Sort by
Telerik team
answered on 11 Jul 2014, 12:46 PM
Hi Evaldas,

RadDocument has two separate states - measured and non-measured. Respectively there are two way to modify these documents - through RadRichTextBox's methods or through the Sections, Blocks, Inlines, etc. collections. That being said, removing elements from a measured (shown in RadRichTextBox) document is not recommended and might cause different issues.

As to your question, you could delete content which is within a read-only range with the help of RadDocumentEditor. All you need to do is set its RespectReadOnlyRanges property to false and pass it the document of the RadRichTextBox. The following snippet would delete the selected content in RadRichTextBox even if it is in a read-only range.
RadDocumentEditor documentEditor = new RadDocumentEditor(this.radRichTextBox.Document);
documentEditor.RespectReadOnlyRanges = false;

I hope the provided information is helpful.

Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
Top achievements
Rank 1
answered on 16 Jul 2014, 06:51 AM
Hi Petya,

It seems that this solved my problem. But I had to add 
documentEditor.RespectNonDeletableRanges = false;
to your code sample, because my read only range was still not deleted. In .xaml of document it looks like this:
<t:Paragraph StyleName="Heading2" Tag="topic_c8832b5d-22d2-49d4-bdbb-e92607e0c7dc">
  <t:ReadOnlyRangeStart AnnotationID="28" />
  <t:Span Tag="topic_c8832b5d-22d2-49d4-bdbb-e92607e0c7dc" Text="1.1 Vienas vienas" />
  <t:ReadOnlyRangeEnd AnnotationID="28" />

Anyway, thank you for your help.

Regards, Evaldas
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Top achievements
Rank 1
Share this question