EnterKeyBehavior Property

Applies To

TextBox control.

Description

Defines the effect of pressing ENTER in a TextBox.

Syntax

object.EnterKeyBehavior [= Boolean]

The EnterKeyBehavior property syntax has these parts:

Part

Description

object

Required. A valid object.

Boolean

Optional. Specifies the effect of pressing ENTER.


Settings

The settings for Boolean are:

Value

Description

True

Pressing ENTER creates a new line.

False

Pressing ENTER moves the focus to the next object in the tab order (default).


Remarks

The EnterKeyBehavior and MultiLine properties are closely related. The values described above only apply if MultiLine is True. If MultiLine is False, pressing ENTER always moves the focus to the next control in the tab order regardless of the value of EnterKeyBehavior.

The effect of pressing CTRL+ENTER also depends on the value of MultiLine. If MultiLine is True, pressing CTRL+ENTER creates a new line regardless of the value of EnterKeyBehavior. If MultiLine is False, pressing CTRL+ENTER has no effect.

See Also

MultiLine property.

Example

The following example uses the EnterKeyBehavior property to control the effect of ENTER in a TextBox. In this example, the user can specify either a single-line or multiline 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.
  • Two ToggleButton controls named ToggleButton1 and ToggleButton2.
    Private Sub UserForm_Initialize()
        TextBox1.EnterKeyBehavior = True
        ToggleButton1.Caption = "EnterKeyBehavior is True"
        ToggleButton1.Width = 70
        ToggleButton1.Value = True
        
        TextBox1.MultiLine = True
        ToggleButton2.Caption = "MultiLine is True"
        ToggleButton2.Width = 70
        ToggleButton2.Value = True
        
        TextBox1.Height = 100
        TextBox1.WordWrap = True
        TextBox1.Text = "Type your text here. If EnterKeyBehavior is True,"& _
            " press ENTER to start a new line. Otherwise, press SHIFT+ENTER."
    End Sub
    
    Private Sub ToggleButton1_Click()
        If ToggleButton1.Value = True Then
            TextBox1.EnterKeyBehavior = True
            ToggleButton1.Caption = "EnterKeyBehavior is True"
        Else
            TextBox1.EnterKeyBehavior = False
            ToggleButton1.Caption = "EnterKeyBehavior is False"
        End If
    End Sub
    
    Private Sub ToggleButton2_Click()
        If ToggleButton2.Value = True Then
            TextBox1.MultiLine = True
            ToggleButton2.Caption = "MultiLine TextBox"
        Else
            TextBox1.MultiLine = False
            ToggleButton2.Caption = "Single-line TextBox"
        End If
    End Sub