Positioning Cursor in Rich Text Box

3 posts, 0 answers
  1. BruceQ
    BruceQ avatar
    5 posts
    Member since:
    Nov 2010

    Posted 20 Jun 2013 Link to this post

                    var textRangeAll = search.FindAll("\\[");

                    foreach (var textRange in textRangeAll)

                    {

                        if (textRange != null)

                        {

                            DocumentPosition startB = textRange.StartPosition;

                            DocumentPosition endB = textRange.EndPosition;

                            var spaceRange = search.Find("[ ;.,]", startB);                            

                               …

                         }
                    }

    The code above will find all open brackets in a rich text box and loop through them.  In the loop we then find the next space, semicolon, period or comma starting in the position of the open bracket.

    I need to change this to start the find for [ ;.,] two characters AFTER the open bracket.  In other words I need to change startB DocumentPosition variable to two characters after the open bracket.

    How can I do this?

  2. Deyan
    Admin
    Deyan avatar
    135 posts

    Posted 21 Jun 2013 Link to this post

    Hello Bruce,

    Thank you for contacting us about this issue!

    In order to move the DocumentPosition with a single position you can use MoveToNext() method. You may notice that this method returns a boolean indicating whether the move was successful (if there was a valid position to move to).

    Here is some sample code which should solve the issue you are facing:

    RadDocument document = this.radRichTextBox.Document;
    DocumentTextSearch search = new DocumentTextSearch(document);
     
    IEnumerable<TextRange> textRangeAll = search.FindAll("\\[");
    foreach (var textRange in textRangeAll)
    {
        if (textRange != null)
        {
                DocumentPosition startB = textRange.StartPosition;
     
                //Moving as many positions as you need.
                startB.MoveToNext();
                startB.MoveToNext();
                         
                TextRange spaceRange = search.Find("[ ;.,]", startB);
     
                //Use the found item here! For example adding it to selection.
                spaceRange.AddToSelection(document);
        }
    }

    If you have any other concerns about this or any other issue please do not hesitate to contact us back!

    Regards,
    Deyan
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. BruceQ
    BruceQ avatar
    5 posts
    Member since:
    Nov 2010

    Posted 24 Jun 2013 Link to this post

    Thank you!  that worked.
Back to Top