EnterKeyBehavior 和 MultiLine 属性示例

下例用 EnterKeyBehavior 属性来控制在文本框中的按 Enter 的操作效果。在此示例中,用户可以指定单行的或者多行的文本框。

窗体包含:

  • 名为 TextBox1 的文本框。
  • 名为 ToggleButton1 和 ToggleButton2 的两个切换按钮控件。
Sub UserForm_Initialize()
    UserForm_TextBox1.EnterKeyBehavior = True
    UserForm_ToggleButton1.Caption = "EnterKeyBehavior is True"
    UserForm_ToggleButton1.Width = 70
    UserForm_ToggleButton1.Value = True
    
    UserForm_TextBox1.MultiLine = True
    UserForm_ToggleButton2.Caption = "MultiLine is True"
    UserForm_ToggleButton2.Width = 70
    UserForm_ToggleButton2.Value = True
    
    UserForm_TextBox1.Height = 100
    UserForm_TextBox1.WordWrap = True
    UserForm_TextBox1.Text = "Type your text here. If EnterKeyBehavior is True,press Enter to start a new line. Otherwise, press SHIFT+ENTER."
End Sub

Sub UserForm_ToggleButton1_Click()
    If UserForm_ToggleButton1.Value = True Then
        UserForm_TextBox1.EnterKeyBehavior = True
        UserForm_ToggleButton1.Caption ="EnterKeyBehavior is True"
    Else
        UserForm_TextBox1.EnterKeyBehavior = False
        UserForm_ToggleButton1.Caption = "EnterKeyBehavior is False"
    End If
End Sub

Sub UserForm_ToggleButton2_Click()
    If UserForm_ToggleButton2.Value = True Then
        UserForm_TextBox1.MultiLine = True
        UserForm_ToggleButton2.Caption = "MultiLine TextBox"
    Else
        UserForm_TextBox1.MultiLine = False
        UserForm_ToggleButton2.Caption = "Single-line TextBox"
    End If
End Sub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35