Setting cursor at the end of any text of a textbox

Anoushka Seechurn picture Anoushka Seechurn · Dec 6, 2013 · Viewed 186.1k times · Source

I have a text box with a displayed string already in it. To bring the cursor to the textbox I am already doing

txtbox.Focus();

But how do I get the cursor at the end of the string in the textbox ?

Answer

Panu Oksala picture Panu Oksala · Dec 6, 2013

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.