How to: Control the Insertion Point in a Windows Forms TextBox Control

When a Windows 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 the insertion point 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 enable 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_Enter event handler must be bound to the control; for more information, see Creating Event Handlers in Windows Forms.

    Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter  
       TextBox1.SelectionStart = 0  
       TextBox1.SelectionLength = 0  
    End Sub  
    
    private void textBox1_Enter(Object sender, System.EventArgs e) {  
       textBox1.SelectionStart = 0;  
       textBox1.SelectionLength = 0;  
    }  
    
    private:  
       void textBox1_Enter(System::Object ^  sender,  
          System::EventArgs ^  e)  
       {  
          textBox1->SelectionStart = 0;  
          textBox1->SelectionLength = 0;  
       }  
    

Making the Insertion Point Visible by Default

The TextBox insertion point is visible by default in a new form only if the TextBox control is first in the tab order. Otherwise, the insertion point appears only if you give the TextBox the focus with either the keyboard or the mouse.

To make the text box insertion point visible by default on a new form

See also