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

Is text always contained by Spans?

1 Answer 50 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Christoph
Top achievements
Rank 1
Christoph asked on 22 Feb 2012, 03:51 PM
Hi

I need to extract text from a ranges of DocumentElements.
For example the text between a BookmarkRangeStart and its paired BookmarkRangeEnd.

My solution is to walk from NextSibling to NextSibling, check the siblings to be of type Span
and get the text. If they are not Spans I recursively walk through the Children collection
and of the sibling, check these to be spans, if not recurse again etc or quit if there are no Children
unti I reach the BookmarkRangeEnd as the last Sibling.
This seems to work.
Questions:
1) is there an easier / more reliable way to get the text of "range" of elements?
(I use TextFormatter.Export for complete Documents, but not for parts)
2) is text always located inside Spans?
3) Can Spans be nested - If so, does the Text property of the "parent" Span contain
the Text content of all "child" spans? (Like InnerText in HTML)?
4) Is there a straight forward way to get the text from a Table as a CSV / TSV string?

Grettings,
Chris

1 Answer, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 24 Feb 2012, 04:55 PM
Hello Chris,

The approach you have adopted should work correctly. You are right, though, that there is an easier way to accomplish the same - it is to select the element and get the text using the GetSelectedText method of DocumentSelection. Here is how this can be done for Bookmarks and for a Table:

private void GetText_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show(this.radRichTextBox1.Document.Selection.GetSelectedText());
}
 
private void SelectRange_Click(object sender, RoutedEventArgs e)
{
    InlineLayoutBox currentInline = this.radRichTextBox1.Document.CaretPosition.GetCurrentInlineBox();
    List<BookmarkRangeStart> bookmarks = this.radRichTextBox1.Document.GetContainingAnnotationRanges<BookmarkRangeStart>(currentInline.AssociatedInline, true).ToList<BookmarkRangeStart>();
    if (bookmarks.Count > 0)
    {
        this.radRichTextBox1.Document.Selection.SelectAnnotationRange(bookmarks[0]);
    }
}
 
private void SelectTable_Click(object sender, RoutedEventArgs e)
{
    if (this.radRichTextBox1.Document.CaretPosition.IsPositionInsideTable)
    {
        TableCellLayoutBox cellBox = this.radRichTextBox1.Document.CaretPosition.GetCurrentTableCellBox();
        Table table = cellBox.AssociatedTableCell.Row.Table;
        IEnumerable<Inline> inlines = table.EnumerateChildrenOfType<Inline>();
        Inline firstInlineInTable = inlines.First();
        Inline lastInlineInTable = inlines.Last();
        DocumentPosition selectionStart = new DocumentPosition(this.radRichTextBox1.Document);
        selectionStart.MoveToInline(firstInlineInTable.FirstLayoutBox as InlineLayoutBox, 0);
        DocumentPosition selectionEnd = new DocumentPosition(this.radRichTextBox1.Document);
        selectionEnd.MoveToInline(lastInlineInTable.FirstLayoutBox as InlineLayoutBox, 0);
        selectionEnd.MoveToNextInline();
        this.radRichTextBox1.Document.Selection.SetSelectionStart(selectionStart);
        this.radRichTextBox1.Document.Selection.AddSelectionEnd(selectionEnd);
    }
}

To your other questions:
2. Text is always contained in Spans.
3. Spans cannot be nested.

All the best,
Iva Toteva
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
RichTextBox
Asked by
Christoph
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Share this question
or