TextBox Control

Description

Displays information from a user or from an organized set of data.

Remarks

A TextBox is the control most commonly used to display information entered by a user. Also, it can display a set of data, such as a table, query, worksheet, or a calculation result. If a TextBox is bound to a data source, then changing the contents of the TextBox also changes the value of the bound data source.

Formatting applied to any piece of text in a TextBox will affect all text in the control. For example, if you change the font or point size of any character in the control, the change will affect all characters in the control.

The default property for a TextBox is the Value property.

The default event for a TextBox is the Change event.

Properties

AutoSize property, AutoTab property, AutoWordSelect property, BackColor property, BackStyle property, BorderColor property, BorderStyle property, BoundValue property, CanPaste property, ControlSource property, ControlTipText property, CurLine property, CurTargetX property, CurX property, DragBehavior property, DropButtonStyle property, Enabled property, EnterFieldBehavior property, EnterKeyBehavior property, Font object, ForeColor property, Height, Width properties, HelpContextID property, HideSelection property, IMEMode property, IntegralHeight property, LayoutEffect property, Left, Top properties, LineCount property, Locked property, MaxLength property, MouseIcon property, MousePointer property, MultiLine property, Name property, Object property, OldHeight, OldWidth properties, OldLeft, OldTop properties, Parent property, PasswordChar property, ScrollBars property, SelectionMargin property, SelLength property, SelStart property, SelText property, ShowDropButtonWhen property, SpecialEffect property, TabIndex property, TabKeyBehavior property, TabStop property, Tag property, Text property, TextAlign property, TextLength property, Value property, Visible property, WordWrap property.

Methods

Copy method, Cut method, Move method, Paste method, SetFocus method, ZOrder method.

Events

AfterUpdate event, BeforeDragOver event, BeforeDropOrPaste event, BeforeUpdate event, Change event, DblClick event, DropButtonClick event, Enter, Exit events, Error event, KeyDown, KeyUp events, KeyPress event, MouseDown, MouseUp events, MouseMove event,

See Also

ComboBox control.

Example

The following example demonstrates the MultiLine, WordWrap, and ScrollBars properties on a TextBox.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • A TextBox named TextBox1.
  • Four ToggleButton controls named ToggleButton1 through ToggleButton4.
To see the entire text placed in the TextBox, set MultiLine and WordWrap to True by clicking the ToggleButton controls.

When MultiLine is True, you can enter new lines of text by pressing SHIFT+ENTER.

ScrollBars appears when you manually change the content of the TextBox.

Private Sub UserForm_Initialize()
'Initialize TextBox properties and toggle buttons

    TextBox1.Text = "Type your text here. Enter SHIFT+ENTER to move to" _
& " a new line." TextBox1.AutoSize = False ToggleButton1.Caption = "AutoSize Off" ToggleButton1.Value = False ToggleButton1.AutoSize = True TextBox1.WordWrap = False ToggleButton2.Caption = "WordWrap Off" ToggleButton2.Value = False ToggleButton2.AutoSize = True TextBox1.ScrollBars = 0 ToggleButton3.Caption = "ScrollBars Off" ToggleButton3.Value = False ToggleButton3.AutoSize = True TextBox1.MultiLine = False ToggleButton4.Caption = "Single Line" ToggleButton4.Value = False ToggleButton4.AutoSize = True End Sub Private Sub ToggleButton1_Click() 'Set AutoSize property and associated ToggleButton If ToggleButton1.Value = True Then TextBox1.AutoSize = True ToggleButton1.Caption = "AutoSize On" Else TextBox1.AutoSize = False ToggleButton1.Caption = "AutoSize Off" End If End Sub Private Sub ToggleButton2_Click() 'Set WordWrap property and associated ToggleButton If ToggleButton2.Value = True Then TextBox1.WordWrap = True ToggleButton2.Caption = "WordWrap On" Else TextBox1.WordWrap = False ToggleButton2.Caption = "WordWrap Off" End If End Sub Private Sub ToggleButton3_Click() 'Set ScrollBars property and associated ToggleButton If ToggleButton3.Value = True Then TextBox1.ScrollBars = 3 ToggleButton3.Caption = "ScrollBars On" Else TextBox1.ScrollBars = 0 ToggleButton3.Caption = "ScrollBars Off" End If End Sub Private Sub ToggleButton4_Click() 'Set MultiLine property and associated ToggleButton If ToggleButton4.Value = True Then TextBox1.MultiLine = True ToggleButton4.Caption = "Multiple Lines" Else TextBox1.MultiLine = False ToggleButton4.Caption = "Single Line" End If End Sub
Example

The following example sets the z-order of a TextBox, so the user can display the entire TextBox (by bringing it to the front of the z-order) or can place the TextBox behind other controls (by sending it to the back of the z-order).

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • Three TextBox controls named TextBox1 through TextBox3.
  • A ToggleButton named ToggleButton1.
    Private Sub ToggleButton1_Click()
    If ToggleButton1.Value = True Then
        TextBox2.ZOrder (fmTop)          'Place TextBox2 on Top of z-order
        
        'Update ToggleButton caption to identify next state
        ToggleButton1.Caption = "Send TextBox2 to back"
    Else
        TextBox2.ZOrder (1)              'Place TextBox2 on Bottom of z-order
        
        'Update ToggleButton caption to identify next state
        ToggleButton1.Caption = "Bring TextBox2 to front"
    End If
    End Sub
    
    Private Sub UserForm_Initialize()
    'Set up text boxes to show z-order in the form
    TextBox1.Text = "TextBox 1"
    TextBox2.Text = "TextBox 2"
    TextBox3.Text = "TextBox 3"
    
    TextBox1.Height = 40
    TextBox2.Height = 40
    TextBox3.Height = 40
    
    TextBox1.Width = 60
    TextBox2.Width = 60
    TextBox3.Width = 60
    
    TextBox1.Left = 10
    TextBox1.Top = 10
    
    TextBox2.Left = 25      'Overlap TextBox2 on TextBox1
    TextBox2.Top = 25
    
    TextBox3.Left = 40      'Overlap TextBox3 on TextBox2, TextBox1
    TextBox3.Top = 40
    
    ToggleButton1.Value = False
    ToggleButton1.Caption = "Bring TextBox2 to Front"
    ToggleButton1.Left = 10
    ToggleButton1.Top = 90
    ToggleButton1.Width = 50
    ToggleButton1.Height = 50
    
    End Sub
Example

The following example demonstrates the effects of the AutoSize property with a single-line TextBox and a multiline TextBox. The user can enter text into either TextBox and turn AutoSize on or off independently of the contents of the TextBox. This code sample also uses the Text property.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • Two TextBox controls named TextBox1 and TextBox2.
  • A ToggleButton named ToggleButton1.
    Private Sub UserForm_Initialize()
        TextBox1.Text = "Single-line TextBox. Type your text here."
        
        TextBox2.MultiLine = True
        TextBox2.Text = "Multi-line TextBox. Type your text here. Use CTRL+ENTER to start a new line."
        
        ToggleButton1.Value = True
        ToggleButton1.Caption = "AutoSize On"
        TextBox1.AutoSize = True
        TextBox2.AutoSize = True
    End Sub
    
    Private Sub ToggleButton1_Click()
        If ToggleButton1.Value = True Then
            ToggleButton1.Caption = "AutoSize On"
            TextBox1.AutoSize = True
            TextBox2.AutoSize = True
        Else
            ToggleButton1.Caption = "AutoSize Off"
            TextBox1.AutoSize = False
            TextBox2.AutoSize = False
        End If
    End Sub
Example

The following example tracks the CurLine, CurTargetX, and CurX property settings in a multiline TextBox. These settings change in the KeyUp event as the user types into the Text property, moves the insertion point, and extends the selection using the keyboard.

To use this example, follow these steps:

  1. Copy this sample code to the Declarations portion of a form.
  2. Add one large TextBox named TextBox1 to the form.
  3. Add three TextBox controls named TextBox2, TextBox3, and TextBox4 in a column.
    Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, _
    ByVal Shift As Integer) TextBox2.Text = TextBox1.CurLine TextBox3.Text = TextBox1.CurX TextBox4.Text = TextBox1.CurTargetX End Sub Private Sub UserForm_Initialize() TextBox1.MultiLine = True TextBox1.Text = "Type your text here. User CTRL + ENTER to start" _
    & " a new line." End Sub
Example

The following example uses the Cut and Paste methods to cut text from one TextBox and paste it into another TextBox.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • Two TextBox controls named TextBox1 and TextBox2.
  • A CommandButton named CommandButton1.
    Private Sub UserForm_Initialize()
        TextBox1.Text = "From TextBox1!"
        TextBox2.Text = "Hello "
        
        CommandButton1.Caption = "Cut and Paste"
        CommandButton1.AutoSize = True
    End Sub
    
    Private Sub CommandButton1_Click()
        TextBox2.SelStart = 0
        TextBox2.SelLength = TextBox2.TextLength
        TextBox2.Cut
    
        TextBox1.SetFocus
        TextBox1.SelStart = 0
        
        TextBox1.Paste
        TextBox2.SelStart = 0
    End Sub