Moving the input character position in a textbox

Requirement

When I write a string to a text box from a datatable, the cursor position is at the beginning of the text. (By cursor position I mean the character position for entry of new characters typed in to the textbox by the user.)

How do I move the cursor position to the end of the text so the user can automatically continue adding text to the end of the text already in the textbox

Problem Cause

My process handles new rows of a form by creating a new row in a datatable for the new row. It then clears all the textboxes in the row and redisplays them. This is triggered by the user typing in the first character, in any text field of a new row. All the subsequent writing and redisplaying actions above occur behind the scenes without the user knowing.

Unfortunately after the re-writing of the text box from the table, i.e. the textbox has been re-written with the original user's first character, the cursor position within this textbox has moved to the front of the text so the next character entered by the user is put in front of the first (original character).

Ideally I would like to re-writing the textbox, then send the cursor position to the end of the text so the user can continue typing the characters in the correct order.




Answer this question

Moving the input character position in a textbox

  • ayr

    In order to change the insertion point within the textbox simply change the SelectionStart property to the character position within the Text, since you want to be at the end that would be the entire length of the Text so if you used:

    textBox1.SelectionStart = textBox1.Text.Length;

    Just after you set the Text property to something you should achieve what you are looking for.



  • Steve at Eglin

    Hey Brendan, thanks a lot mate - saved me quite a long work around.

    Visited your site - very amusing - sorry to hear about you're plight!. Will put a small consideration in the post for your help.

    Cheers

    John



  • Moving the input character position in a textbox