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 ?
You need to set Cursor property when Drop event occur. You can set it by txt. Cursor where txt is your textbox. Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
To position the cursor at the end of the contents of a TextBox control, call the Select method and specify the selection start position equal to the length of the text content, and a selection length of 0.
selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range range. collapse(false);//collapse the range to the end point. false means collapse to end rather than the start selection = window. getSelection();//get the selection object (allows you to change selection) selection.
set autofocus="autofocus" in the textbox. This will work in HTML 5.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With