In Focus

Positioning Caret in Textbox

This article explains how to position a caret in text using code.

Introduction

In this article we will learn how to position the caret programmatically in a text box. It may be useful in various situations. In this article we will cover the five positions:

  1. Caret at beginning.
  2. Caret at End.
  3. Caret at Specific position.
  4. Caret at Start of the line.
  5. Caret at the end of he line.

So let's start with the first one.

Caret at beginning

Use this code to place the caret at the beginning of the text block.

private void moveToBeg()

{

     textBox1.Select(0, 0);

     textBox1.Focus();

     textBox1.ScrollToCaret();

}

Calling this method will place your caret at the beginning of the text box. The Select method is used to select a specific text with the first parameter as the starting position and the second parameter is the length of the selection. 0,0 will place the caret at the beginning. The Focus() will be given to the TextBox focus and scrollToCaret will scroll the scrollbar to an appropriate position.

Caret at end

Use the following code to move the caret to the end of the text.

private void moveToEnd()

{

     textBox1.Select(textBox1.Text.Length, 0);

     textBox1.Focus();

     textBox1.ScrollToCaret();

}

Calling the preceding method will place the caret at the end of the text. In this method the select is used with the first parameter as the total length of the text and the second parameter, zero, is the length of the selected text.

Caret at Specific position

In this method we are moving the caret to a specific character position passed in the method. Check the following code:

private void moveAt(int charsFromStart)

{

     textBox1.Select(charsFromStart, 0);

     textBox1.Focus();

     textBox1.ScrollToCaret();

}

 
So if you send 15 as the parameter then the caret will be at 15 chars away from the start.

Caret at beginning of the line

The following code will move the caret to the beginning of the line. You need to pass the line number as parameter.

  private void moveToLineStart(int lineNumber)

  {

      int skipChars = textBox1.GetFirstCharIndexFromLine(lineNumber);

      textBox1.Select(skipChars, 0) ;

      textBox1.Focus();

      textBox1.ScrollToCaret();

}

In this function we are extracting the index of the first character from the given line using the GetFirstCharIndexFromLine method. Next we are using the select to move the caret to that char.

Caret at end of the line

This function moves the caret to the end of the line. It takes line number as a parameter.

 private void moveToLineEnd(int lineNumber)

 {

       int skipChars = textBox1.GetFirstCharIndexFromLine(lineNumber+1);

        if (skipChars == -1)   //to check for last line

        {

             textBox1.Select(textBox1.Text.Length,0);

        }

        else

        {

            textBox1.Select(skipChars - 1, 0);                   

        }

        textBox1.Focus();

        textBox1.ScrollToCaret();

}

In this function we are first finding the index of the first char for the next line of the line on which we want to place our caret. Next we are placing the caret at one less than the first char index of the next line. That will put the caret at the end of the line.

Output