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

Removing Section/blocks from a RadDocument

4 Answers 188 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Shahrzade
Top achievements
Rank 1
Shahrzade asked on 14 Apr 2016, 01:35 PM

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.

4 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 15 Apr 2016, 01:39 PM
Hi ,

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.
0
Jeffrey
Top achievements
Rank 1
answered on 28 Sep 2018, 08:19 PM

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.

0
Jeffrey
Top achievements
Rank 1
answered on 28 Sep 2018, 08:33 PM

Never mind. I found using this method works:

desRichTextBoxEditor.Document = New RadDocument

0
Hristo
Telerik team
answered on 01 Oct 2018, 10:42 AM
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.
Tags
RichTextEditor
Asked by
Shahrzade
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Jeffrey
Top achievements
Rank 1
Share this question
or