How can you programmatically position the cursor on a given line or on a character in the RichTextBox control in C#?

The RichTextBox control contains the Lines array property, which displays one item of an array in a separate line. Each line entry has a Length property, which can be used to accurately position the cursor at a character, as shown in the following code :

private void GoToLineAndColumn(RichTextBox RTB, int Line, int Column)
{
 int offset = 0;
 for(int i = 0; i < Line -1 && i < RTB.Lines.Length; i++)
 {
 offset += RTB.Lines[i].Length + 1;
 }
 RTB.Focus();
 RTB.Select(offset + Column, 0);
}

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*