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

Positioning Cursor in Rich Text Box

2 Answers 94 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
BruceQ
Top achievements
Rank 1
BruceQ asked on 20 Jun 2013, 03:34 PM

                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 Answers, 1 is accepted

Sort by
0
Deyan
Telerik team
answered on 21 Jun 2013, 12:29 PM
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.

0
BruceQ
Top achievements
Rank 1
answered on 24 Jun 2013, 04:06 PM
Thank you!  that worked.
Tags
RichTextBox
Asked by
BruceQ
Top achievements
Rank 1
Answers by
Deyan
Telerik team
BruceQ
Top achievements
Rank 1
Share this question
or