Position the cursor at the end of any text field

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

txtbox.Focus(); 

But how do I get the cursor at the end of a line in a text box?

+43
c # wpf cursor-position textbox
Dec 06 '13 at 11:52
source share
4 answers

You can control the cursor position (and selection) using the txtbox.SelectionStart and txtbox.SelectionLength . If you want the carriage to end, try this:

 txtbox.SelectionStart = txtbox.Text.Length -1 // add some logic if length is 0 txtbox.SelectionLength = 0 

For WPF, see this post .

+63
Dec 06 '13 at
source share

There are several 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); 
+42
Dec 06 '13 at 11:57
source share

You can set the carriage position using TextBox.CaretIndex . If you only need to position the cursor at the end, you can simply pass the length of the string, for example:

 txtBox.CaretIndex=txtBox.Text.Length; 

You need to set the carriage index in length, not in length-1, because this will put the carriage in front of the last character.

+9
Dec 06 '13 at 11:57
source share

Try as shown below ... this will help you ...

For some time, Window Form Focus() is not working properly. Therefore, you can use Select() to focus the text field.

 txtbox.Select(); // to Set Focus txtbox.Select(txtbox.Text.Length, 0); //to set cursor at the end of textbox 
0
Dec 06 '13 at 12:01
source share



All Articles