Skip to content

Latest commit

 

History

History
70 lines (55 loc) · 3.98 KB

how-to-control-the-insertion-point-in-a-windows-forms-textbox-control.md

File metadata and controls

70 lines (55 loc) · 3.98 KB
title ms.date dev_langs helpviewer_keywords ms.assetid description
Control the Insertion Point in TextBox Control
03/30/2017
csharp
vb
cpp
TextBox control [Windows Forms], insertion point
insertion points [Windows Forms], TextBox controls
text boxes [Windows Forms], controlling insertion point
5bee7d34-5121-429e-ab1f-d8ff67bc74c1
Learn how to control the insertion point in a Windows Forms TextBox control by setting the SelectionStart property to an appropriate value.

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

When a Windows Forms xref:System.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 xref:System.Windows.Forms.TextBoxBase.SelectionStart%2A and xref:System.Windows.Forms.TextBoxBase.SelectionLength%2A properties enable you to modify the behavior to suit your purpose.

To control the insertion point in a TextBox control

  1. Set the xref:System.Windows.Forms.TextBoxBase.SelectionStart%2A property to an appropriate value. Zero places the insertion point immediately to the left of the first character.

  2. (Optional) Set the xref:System.Windows.Forms.TextBoxBase.SelectionLength%2A 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 xref:System.Windows.Forms.TextBox insertion point is visible by default in a new form only if the xref:System.Windows.Forms.TextBox control is first in the tab order. Otherwise, the insertion point appears only if you give the xref:System.Windows.Forms.TextBox the focus with either the keyboard or the mouse.

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

  • Set the xref:System.Windows.Forms.TextBox control's xref:System.Windows.Forms.Control.TabIndex%2A property to 0.

See also