New to Telerik UI for WinForms? Download free 30-day trial

Caret positioning and selection

The positioning and selection in RadTextBoxControl can be performed programmatically as well as using keyboard and mouse input.

To select text you can press the shift key followed by left mouse button or one of the navigation keys of the keyboard (up, down, left, right buttons).

Programmatically selection can be performed by using the SelectionStart and SelectionLength properties of RadTextBoxControl.

The SelectionStart property is a number that indicates the insertion point within the string of text, with 0 being the left-most position. If the SelectionStart property is set to a value equal to or greater than the number of characters in the text box, the insertion point is placed after the last character.

The SelectionLength property is a numeric value that sets the width of the insertion point. Setting the SelectionLength to a number greater than 0 causes that number of characters to be selected, starting from the current insertion point.

Select part of the text using the SelectionStart and SelectionLenght properties.


private void SetSelection()
{
    this.radTextBoxControl1.Text = "Hello, John Green";
    this.radTextBoxControl1.SelectionStart = 7;
    this.radTextBoxControl1.SelectionLength = 4;
}

Private Sub SetSelection()
    Me.RadTextBoxControl1.Text = "Hello, John Green"
    Me.RadTextBoxControl1.SelectionStart = 7
    Me.RadTextBoxControl1.SelectionLength = 4
End Sub

Use the Select method to select the part of the text:

private void SelectText()
{
    this.radTextBoxControl1.Text = "Hello, John Green";
    this.radTextBoxControl1.Select(7, 4);
}

Private Sub SelectText()
    Me.RadTextBoxControl1.Text = "Hello, John Green"
    Me.RadTextBoxControl1.[Select](7, 4)
End Sub

The both approaches produce same result:

WinForms RadTextBoxControl Caret Position Selection

You can access the selected text through the SelectedText property. To select the whole text use the SelectAll method.

Note that you can use the CaretIndex property to move the caret at specified position and clear the selection.

See Also

In this article