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

4

100

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 ?

Ol answered 6/12, 2013 at 11:52 Comment(2)
is it WPF or winformsAvirulent
May this helps: #7751005Hurl
I
170

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.

Itch answered 6/12, 2013 at 11:56 Comment(9)
Which post says NOT to use SelectionStart.Bake
I don't think we need -1, if we won't to put the input cursor at the end of textbox.Urinalysis
Math.max(0, txtbox.Text.Length -1); // Math.max is the logic if length is 0Columbarium
@YuchenZhong is right, -1 puts the insertion point before the last character (her|e) instead of after it (here|). Also, text boxes have a .TextLength property so all you need is txtBox.SelectionStart = txtbox.TextLength.Indispose
@PanagiotisKanavos the post he links is for WPF, whereas the answer given is for Windows Forms.Indispose
@Indispose exactly. The question is tagged WPF but the answer is about Windows Forms. WPF allows caret positioning directlyBake
@PanagiotisKanavos Oops, I missed that the question was WPF...Indispose
This worked for me, thanks.Lager
Worked for me. Good for if contents of the TextBox (e.g. path to a file) is too long for the displayed box and you actually need to see the filenameEunaeunice
R
80

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);
Robomb answered 6/12, 2013 at 11:57 Comment(1)
That sets the selection, not the cursor positionBake
B
12

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.

Bake answered 6/12, 2013 at 11:57 Comment(0)
P
1

Try like below... it will help you...

Some time in Window Form Focus() doesn't work correctly. So better you can use Select() to focus the textbox.

txtbox.Select(); // to Set Focus
txtbox.Select(txtbox.Text.Length, 0); //to set cursor at the end of textbox
Penner answered 6/12, 2013 at 12:1 Comment(3)
The OP asks about the cursor position, not the selectionBake
@PanagiotisKanavos: Can you read my post correctly before comment...Penner
From the docs TextBox.Select Method: Selects a range of text in the text box. While you can emulate positioning by manipulating the selection, it's better to just place the cursor where you want itBake

© 2022 - 2024 — McMap. All rights reserved.