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

Crash on delete range containing table

3 Answers 187 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 23 Feb 2012, 01:19 PM
If I try to delete a range containing a table, either via setting a selection and Document.Delete(false) or via Document.Delete(start, end), I get an InvalidCastException:

Unable to cast object of type 'Telerik.Windows.Documents.Layout.TableLayoutBox' to type 'Telerik.Windows.Documents.Layout.ParagraphLayoutBox'.

   at Telerik.Windows.Documents.Model.RadDocument.DeleteParagraphsInRangeInternal(DocumentPosition fromPosition, DocumentPosition toPosition) in c:\TB\102\WPF_Scrum\Release_WPF\Sources\Development\Documents\Core\Model\RadDocument.cs:line 4530
   at Telerik.Windows.Documents.Model.RadDocument.DeleteRangeInternal(SelectionRange range) in c:\TB\102\WPF_Scrum\Release_WPF\Sources\Development\Documents\Core\Model\RadDocument.cs:line 4574
   at Telerik.Windows.Documents.Model.RadDocument.DeleteRange(DocumentPosition fromPosition, DocumentPosition toPosition) in c:\TB\102\WPF_Scrum\Release_WPF\Sources\Development\Documents\Core\Model\RadDocument.cs:line 1221

Are there any alternatives or workarounds?  Or am I doing something wrong?

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 28 Feb 2012, 10:52 AM
Hello Charles,

Thank you for the feedback! We will revise our RadDocument API regarding deletion because there is a problem.
Overall, you should refrain from using the methods of RadDocument for modifying the content and use the respective methods of RadRichTextBox. The methods of the editor execute commands, which track the state of the document in order to ensure the correct execution of the operations.

At the current moment having the range deletion task you can use the Delete method of the RadRichTextBox instead of the Document. Here is a sample code:

this.radRichTextBox.InsertTable(2, 2);
this.radRichTextBox.Document.CaretPosition.MoveToFirstPositionInDocument();
DocumentPosition start = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
this.radRichTextBox.Document.CaretPosition.MoveToLastPositionInDocument();
DocumentPosition end = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
this.radRichTextBox.Document.Selection.SetSelectionStart(start);
this.radRichTextBox.Document.Selection.AddSelectionEnd(end);
this.radRichTextBox.Delete(false);

Don't hesitate to contact us if you have other questions.

All the best,
Martin
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Karlheinz
Top achievements
Rank 1
answered on 19 Feb 2013, 06:02 PM
Hello everyone,
first of all I'm using the latest telerik controls for silverlight.
sorry for opening this old thread but I'm experiencing the exact same problem.
You spoke of a problem in the RadDocument Api. Is this fixed for WPF?
Heres is the Structure:

<table>
   <row>
     <cell>
         <paragraph>
            <rangestart>
         </paragraph>
         <table>
          .
          .some other content
          .
         </table>
         <paragraph>
            <rangeend>
         </paragraph>
     </cell>
   </row>
</table>


And the code:

doc.Selection.SelectAnnotationRange(start);
doc.Delete(false); //<-throwing the error

Since above structure is loaded and created inside a custom mergefields GetResultFragment() method. the problem with your workaround for this case is that it is not part of the richtextbox's document at that stage.
Any advise?

Greets,
Andreas

Just checked that my exception message says: null reference exception at:
bei Telerik.Windows.Documents.Model.RadDocument.DeleteSection(ParagraphLayoutBox firstParagraph, ParagraphLayoutBox secondParagraph)
bei Telerik.Windows.Documents.Model.RadDocument.DeleteInternal(DocumentPosition documentPosition, Boolean deletePrevious)
bei Telerik.Windows.Documents.Model.RadDocument.DeleteRangeInternal(SelectionRange range)
bei Telerik.Windows.Documents.Model.RadDocument.DeleteInternal(Boolean deletePrevious)
bei Telerik.Windows.Documents.Model.RadDocument.Delete(Boolean deletePrevious)
bei TriasPortal.CustomMergeField.insertMk(RadDocument doc)
bei TriasPortal.CustomMergeField.GetResultFragment()
0
Petya
Telerik team
answered on 22 Feb 2013, 02:15 PM
Hello Karlheinz,

Thank you for contacting us!

Actually, the issue Martin was referring to was resolved by exposing a way to modify a document without using the RadDocument API. What I mean is, you can create a RadDocumentEditor instance and pass the document you are modifying to it. This mimics the behavior of the document being actually shown in an editor and you can use the RadRichTextBox API instead:
RadDocumentEditor myEditor = new RadDocumentEditor(doc);
myEditor.Document.History.IsEnabled = false;
myEditor.Document.Selection.SelectAnnotationRange(editor.Document.GetBookmarkByName("bm"));
 
myEditor.Delete(false);

The API was introduced in our latest official release Q1 2013, so you'd have to upgrade your project in order to use it.

I hope this is helpful! Let us know if there is anything else we can assist you with.
 
Regards,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
RichTextBox
Asked by
Charles
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Karlheinz
Top achievements
Rank 1
Petya
Telerik team
Share this question
or