NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Controlling the Insertion Point in a TextBox Control

When a Win Forms TextBox control first receives the focus, the default insertion within the text box is to the left of any existing text. The user can move it with the keyboard or the mouse. If the text box loses and then regains the focus, the insertion point will be wherever the user last placed it.

In some cases, this behavior can be disconcerting to the user. In a word processing application, the user might expect new characters to appear after any existing text. In a data entry application, the user might expect new characters to replace any existing entry. The SelectionStart and SelectionLength properties allow you to modify the behavior to suit your purpose.

To control the insertion point in a TextBox control

  1. Set the SelectionStart property to an appropriate value. Zero places the insertion point immediately to the left of the first character.
  2. (Optional) Set the SelectionLength property to the length of the text you want to select.

The code below always returns the insertion point to 0. The TextBox1_GotFocus event handler must be bound to the control; see Creating Event Handlers at Run Time for Win Forms for more information.

[Visual Basic]
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As EventArgs)
   TextBox1.SelectionStart = 0
   TextBox1.SelectionLength = 0
End Sub
[C#]

See Also

Creating a Password Text Box with the TextBox Control | Creating a Read-Only Text Box | Putting Quotation Marks in a String Programmatically | Selecting Text Programmatically in the TextBox Control | Viewing Multiple Lines in the TextBox Control | TextBox Control