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

Caretposition, Table, Images, etc

1 Answer 95 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
JoRoSch
Top achievements
Rank 1
JoRoSch asked on 19 Dec 2011, 10:55 AM
Hi everybody,
as a newbie to Telerik controls i have some questions concerning RadRichTextbox.
@1: I need the current caretposition as an integer value. How do I do that?
@2: I need the total amount of characters in an document. Is exporting it in TxtFormat and then GetAllText.Length the right way?
@3: I need the positions of all tables and images in a document (int values). How do I do that? 
@4: I need to set a selection programmatically with the parameters (int startPos, int length). How do I do that?

Thank you in advance 
jorosch

1 Answer, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 22 Dec 2011, 02:12 PM
Hello,
  1. Actually document position cannot be represented as a simple integer, rather by the so called hierarchical index - this is due to the complex nature of the document, which may contain nested tables/paragraphs. The following code shows how to preserve the caret position and then move the caret to the preserved position:
    private int[] hierachicanlIndexArray;
    private int currentPositionInSpan;
      
    private void buttonSave_Click(object sender, RoutedEventArgs e)
    {
        this.hierachicanlIndexArray = HierarchicalIndex.GetHierarchicalIndexByBox(this.radRichTextBox.Document.CaretPosition.GetCurrentInlineBox()).GetIndexArray();
        this.currentPositionInSpan = this.radRichTextBox.Document.CaretPosition.GetCurrentPositionInSpan();
      
        this.radRichTextBox.Focus();
    }
      
    private void buttonRestore_Click(object sender, RoutedEventArgs e)
    {
        HierarchicalIndex index = new HierarchicalIndex(hierachicanlIndexArray);
        InlineLayoutBox inlineLayotBox = (InlineLayoutBox)HierarchicalIndex.GetBoxByHierarchicalIndex(this.radRichTextBox.Document.DocumentLayoutBox, index);
        this.radRichTextBox.Document.CaretPosition.MoveToInline(inlineLayotBox, this.currentPositionInSpan);
      
        this.radRichTextBox.Focus();
    }
  2. This is the right way to do it. Note that TxtFormatPprovider will strip all none-textual elements, like images, tables etc.
  3. You can use the hierarchical indices (as demonstrated in 1.), but if you need the positions just for navigation, you can consider using bookmarks in your document instead.
  4. You can create selection programmatically as described in this help article.
If some of the points doesn't answer your question, you could share more details about your scenario, so we can more adequately help you.
Kind regards,
Boby
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
JoRoSch
Top achievements
Rank 1
Answers by
Boby
Telerik team
Share this question
or