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

Get paragraphs from a RadDocument

3 Answers 377 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kirmer
Top achievements
Rank 1
Kirmer asked on 30 Nov 2010, 03:14 PM
Hi, how can I get the paragraphs from a RadDocument? (I'd like to remove the last empty lines/enters/paragraphs from the text..)

Thanks,
Tamas










3 Answers, 1 is accepted

Sort by
0
Ivailo Karamanolev
Telerik team
answered on 30 Nov 2010, 04:42 PM
Hi Kirmer,

In Q3 we have introduced a method in every DocumentElement (RadDocument, including, being DocumentElement itself) called EnumerateChildrenOfType<T>() which recursively traverses all child elements of type T. You can foreach through all paragraphs like this:
foreach(Paragraph paragraph in document.EnumerateChildrenOfType<Paragraph>()) {
    ...
}
Let us know if you require any further assistance.

Kind regards,
Ivailo
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Kirmer
Top achievements
Rank 1
answered on 30 Nov 2010, 09:24 PM
Hi Ivalio,

Thanks for the answer, it works. And how can I delete the actual paragraph inside the foreach?

Thanks and regards,
Tamas
0
Iva Toteva
Telerik team
answered on 02 Dec 2010, 09:14 AM
Hi Kirmer,

Since you cannot remove the elements of a collection in a foreach cycle, you would need to find which elements you want to delete at one pass and actually delete them afterwards. Something like:

private void deleteEmptyParagraphs_Click(object sender, RoutedEventArgs e)
{
    BlockCollection blocks = this.radRichTextBox1.Document.Sections.First.Blocks;
    List<Block> toDelete = new List<Block>();
    foreach (Block current in blocks)
    {
        if (current is Paragraph)
        {
             Paragraph currentParagraph = (Paragraph)current;
             if (...)
             {
                  toDelete.Add(currentParagraph);
             }
             else
             {
                  toDelete = new List<Block>();
             }
         }
     }
     foreach (Block block in toDelete)
     {
         blocks.Remove(block);
     }
 }

Let us know if you need anything else.

All the best,
Iva
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
RichTextBox
Asked by
Kirmer
Top achievements
Rank 1
Answers by
Ivailo Karamanolev
Telerik team
Kirmer
Top achievements
Rank 1
Iva Toteva
Telerik team
Share this question
or