Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 576
Anoushka Seechurn Avatar asked Dec 06 '13 11:12

Anoushka Seechurn


People also ask

How do you change the cursor on a text box?

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.

How do you move the cursor to the end of the text in a textbox C#?

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.

How do you move the cursor to the end of Contenteditable entity?

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.

How do you get the cursor position in a textbox in HTML?

set autofocus="autofocus" in the textbox. This will work in HTML 5.


2 Answers

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.

like image 77
Panu Oksala Avatar answered Oct 16 '22 02:10

Panu Oksala


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); 
like image 31
Vishal Suthar Avatar answered Oct 16 '22 01:10

Vishal Suthar