Simple selection given two numeric positions

6 posts, 1 answers
  1. xavendano
    xavendano avatar
    35 posts
    Member since:
    Feb 2012

    Posted 11 Jul 2012 Link to this post

    Hi, all....

    There is a RadRichTextBox and want to select text from the Document object based on two positions, 2 parameters: initial position of the character and end character position. That is, the two parameters are only numbers.

    Is this possible?

    Thanks alls
  2. Boby
    Admin
    Boby avatar
    595 posts

    Posted 16 Jul 2012 Link to this post

    Hi Xavendano,
    You can use DocumentTextMap class for mapping text positions to document positions with the help of MapFromIndexInText method.

    Kind regards,
    Boby
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. xavendano
    xavendano avatar
    35 posts
    Member since:
    Feb 2012

    Posted 16 Jul 2012 Link to this post

    Hi Boby and thanks....I try the following code (vb,net / WPF WebBrowser Application):

            1: Dim documentMap As New DocumentTextMap(Me.myRadRichTextBox.Document)
            2: Dim textRange As TextRange = documentMap.MapFromIndexInText(2, 4)

            3: Me.myRadRichTextBox.Document.Selection.AddSelectionStart(textRange.StartPosition)
            4: Me.myRadRichTextBox.Document.Selection.AddSelectionStart(textRange.EndPosition)

    The line 2 show the following error:

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Why?  Because the line 1 while Im creating the documentMap Object, this is empty:

    Error - No children available.

    But, the radRich control I'snt not empty, they had a single line with multiple words.....why the line 1 do that??

    How can I fix this?

    Thanks alls 

  5. Answer
    Martin Ivanov
    Admin
    Martin Ivanov avatar
    104 posts

    Posted 19 Jul 2012 Link to this post

    Hello Romell,

    You need to initialize the map with document positions. Also your selection must have start and end.
    Here is an example:

    Dim documentMap As New DocumentTextMap(Me.myRadRichTextBox.Document)
          Dim pos1 As New DocumentPosition(Me.myRadRichTextBox.Document)
          Dim pos2 As New DocumentPosition(Me.myRadRichTextBox.Document)
          pos2.MoveToNextInline()
          pos2.MoveToNextInline()
     
          documentMap.InitMap(pos1, pos2)
     
          Dim textRange As TextRange = documentMap.MapFromIndexInText(1, 5)
     
          Me.myRadRichTextBox.Document.Selection.AddSelectionStart(textRange.StartPosition)
          Me.myRadRichTextBox.Document.Selection.AddSelectionEnd(textRange.EndPosition)


    Kind regards,
    Martin
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. xavendano
    xavendano avatar
    35 posts
    Member since:
    Feb 2012

    Posted 21 Aug 2012 Link to this post

    Hi Martin,

    how I can get the exact opposite?

    I mean, given a choice of a text in a RadRichTextBox, how I can get the first and second index that defines the selection made.

    Example: "THE HOUSE IS GREEN"
    When I select the word "HOUSE", I get :   pos1 = 4 and pos2 = 9.

    Thanks ...
  7. xavendano
    xavendano avatar
    35 posts
    Member since:
    Feb 2012

    Posted 29 Aug 2012 Link to this post

    Mmmmm   Any ideas???
Back to Top
UI for WPF is Visual Studio 2017 Ready