Setting cursor at the end of any text of a textbox [duplicate] Setting cursor at the end of any text of a textbox [duplicate] wpf wpf

Setting cursor at the end of any text of a textbox [duplicate]


For Windows Forms you can control cursor position (and selection) with txtbox.SelectionStart and txtbox.SelectionLength properties. If you want to set caret to end try this:

txtbox.SelectionStart = txtbox.Text.Length;txtbox.SelectionLength = 0;

For WPF see this question.


There are multiple options:

txtBox.Focus();txtBox.SelectionStart = txtBox.Text.Length;

OR

txtBox.Focus();txtBox.CaretIndex = txtBox.Text.Length;

OR

txtBox.Focus();txtBox.Select(txtBox.Text.Length, 0);


You can set the caret position using TextBox.CaretIndex. If the only thing you need is to set the cursor at the end, you can simply pass the string's length, eg:

txtBox.CaretIndex=txtBox.Text.Length;

You need to set the caret index at the length, not length-1, because this would put the caret before the last character.