Crash on delete range containing table

4 posts, 0 answers
  1. Charles
    Charles avatar
    15 posts
    Member since:
    Feb 2012

    Posted 23 Feb 2012 Link to this post

    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?
  2. Martin Ivanov
    Martin Ivanov avatar
    104 posts

    Posted 28 Feb 2012 Link to this post

    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);
    DocumentPosition start = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
    DocumentPosition end = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);

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

    All the best,
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. Karlheinz
    Karlheinz avatar
    1 posts
    Member since:
    Dec 2012

    Posted 19 Feb 2013 Link to this post

    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:

              .some other content

    And the code:

    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?


    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()
  4. Petya
    Petya avatar
    983 posts

    Posted 22 Feb 2013 Link to this post

    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;

    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.
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top