Removing Section/blocks from a RadDocument

5 posts, 0 answers
  1. Shahrzade
    Shahrzade avatar
    8 posts
    Member since:
    Feb 2016

    Posted 14 Apr 2016 Link to this post

    Hi,

    I have a uerControl, MyUserControl, that contains a RadRichTextEditor. The method that initializes MyUserControl instantiates a new RadDocument for the RadRichTextEditor. The new document is sometimes empty (no sections) and sometimes has one section that contains a paragrph!!! I have the following code to remove the initial paragraph (2 versions) and they both crash. What is the right way of doing it?

    public void InitMyUserControl()
    {
         textEditor.RichTextBoxElement.BackColor = Color.White;
          textEditor.Document = new RadDocument();
          if ( textEditor.Document.Sections.Count != 0)
          {
                var block =  textEditor.Document.Sections.First.Blocks.First;
                 textEditor.Document.Sections.First.Blocks.Remove(block);
          }

          OR

          if ( textEditor.Document.Sections.Count != 0)
          {
                Section section =  textEditor.Document.Sections.First;
                 textEditor.Document.Sections.Remove(section);
          }
    }

    Thanks,

    Shahrzade.

  2. Hristo
    Admin
    Hristo avatar
    1508 posts

    Posted 15 Apr 2016 Link to this post

    Hi Shahrzade,

    Thank you for writing.

    Deleting the sections or blocks in this manner leaves the editor in an invalid state. In order to accomplish your task, you can call the Clear method of the Blocks and Sections collections respectively:
    this.radRichTextEditor1.Document.Sections.First.Blocks.Clear();
    this.radRichTextEditor1.Document.Sections.Clear();

    In case, you would like to delete the contents of the editor you can perform a selection and then clear the contents:
    this.radRichTextEditor1.Document.Selection.SelectAll();
    this.radRichTextEditor1.RichTextBoxElement.Delete(true);

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. Jeffrey
    Jeffrey avatar
    6 posts
    Member since:
    Jun 2018

    Posted 28 Sep 2018 in reply to Hristo Link to this post

    Hi,

    I'm trying to clear all content in a radrichtexteditor that has been loaded with XAML text form a database using this code:

    Me.desRichTextBoxEditor.Document = xamlformatProvider.Import(myData.Rows(0).Item("XAML"))

     

    The document loads and works fine. However, if I want to clear all the text/content of the radrichtexteditor, using your code it will select all the text but will not delete anything. 

    Just looking for a simple way to clear the richtextboxeditor of content for the user to start fresh with nothing in there. Thanks.

  4. Jeffrey
    Jeffrey avatar
    6 posts
    Member since:
    Jun 2018

    Posted 28 Sep 2018 in reply to Jeffrey Link to this post

    Never mind. I found using this method works:

    desRichTextBoxEditor.Document = New RadDocument

  5. Hristo
    Admin
    Hristo avatar
    1508 posts

    Posted 01 Oct 2018 Link to this post

    Hi Jeffrey,

    Selecting and clearing the content will not delete the document and any styles it might bring will remain. As you would like to let the end user start from fresh, indeed, creating a new document is a good solution. Let me know if you will be needing further assistance.

    Regards,
    Hristo
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top