Caretposition, Table, Images, etc

2 posts, 0 answers
  1. JoRoSch
    JoRoSch avatar
    8 posts
    Member since:
    Dec 2011

    Posted 19 Dec 2011 Link to this post

    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 
  2. Boby
    Boby avatar
    685 posts

    Posted 22 Dec 2011 Link to this post

    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();
      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);
    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,
    the Telerik team

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

Back to Top