* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
3.8 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Control the Insertion Point in TextBox Control | 03/30/2017 |
|
|
5bee7d34-5121-429e-ab1f-d8ff67bc74c1 |
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
-
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.
-
(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_Enterevent 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 Subprivate 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
- xref:System.Windows.Forms.TextBox
- TextBox Control Overview
- How to: Create a Password Text Box with the Windows Forms TextBox Control
- How to: Create a Read-Only Text Box
- How to: Put Quotation Marks in a String
- How to: Select Text in the Windows Forms TextBox Control
- How to: View Multiple Lines in the Windows Forms TextBox Control
- TextBox Control