AutoSize、MultiLine、WordWrap、ScrollBars 属性和文本框控件示例

下例在文本框上演示 MultiLine、WordWrap 和 ScrollBars 属性。

窗体包含:

  • 名为 TextBox1 的文本框。
  • 名为 ToggleButton1 到 ToggleButton4 的四个切换按钮控件。

若要浏览文本框中的整个文本,请单击切换按钮控件,将 MultiLine 和 WordWrap 设置为 True。

当 MultiLine 为 True 时,按 Shift+Enter 可以输入新的文本行。

手动改变文本框的内容时,会出现滚动条。

Sub UserForm_Initialize()
'Initialize TextBox properties and toggle buttons

    UserForm_TextBox1.Text = "Type your text here. Enter SHIFT+ENTER to move to a new line."

    UserForm_TextBox1.AutoSize = False
    UserForm_ToggleButton1.Caption = "AutoSize Off"
    UserForm_ToggleButton1.Value = False
    UserForm_ToggleButton1.AutoSize = True

    UserForm_TextBox1.WordWrap = False
    UserForm_ToggleButton2.Caption = "WordWrap Off"
    UserForm_ToggleButton2.Value = False
    UserForm_ToggleButton2.AutoSize = True

    UserForm_TextBox1.ScrollBars = 0
    UserForm_ToggleButton3.Caption = "ScrollBars Off"
    UserForm_ToggleButton3.Value = False
    UserForm_ToggleButton3.AutoSize = True

    UserForm_TextBox1.MultiLine = False
    UserForm_ToggleButton4.Caption = "Single Line"
    UserForm_ToggleButton4.Value = False
    UserForm_ToggleButton4.AutoSize = True
 End Sub

Sub UserForm_ToggleButton1_Click()
'Set AutoSize property and associated ToggleButton

    If UserForm_ToggleButton1.Value = True Then
        UserForm_TextBox1.AutoSize = True
        UserForm_ToggleButton1.Caption = "AutoSize On"
    Else
        UserForm_TextBox1.AutoSize = False
        UserForm_ToggleButton1.Caption = "AutoSize Off"
    End If
End Sub
Sub UserForm_ToggleButton2_Click()
'设置 WordWrap 属性和相关的 ToggleButton

    If UserForm_ToggleButton2.Value = True Then
        UserForm_TextBox1.WordWrap = True
        UserForm_ToggleButton2.Caption = "WordWrap On"
    Else
        UserForm_TextBox1.WordWrap = False
        UserForm_ToggleButton2.Caption = "WordWrap Off"
    End If
End Sub
Sub UserForm_ToggleButton3_Click()
'设置 ScrollBars 属性和相关的 ToggleButton

    If UserForm_ToggleButton3.Value = True Then
        UserForm_TextBox1.ScrollBars = 3
        UserForm_ToggleButton3.Caption = "ScrollBars On"
    Else
        UserForm_TextBox1.ScrollBars = 0
        UserForm_ToggleButton3.Caption = "ScrollBars Off"
    End If
End Sub
Sub UserForm_ToggleButton4_Click()
'设置 MultiLine 属性和相关的 ToggleButton

    If UserForm_ToggleButton4.Value = True Then
        UserForm_TextBox1.MultiLine = True
        UserForm_ToggleButton4.Caption = "Multiple Lines"
    Else
        UserForm_TextBox1.MultiLine = False
        UserForm_ToggleButton4.Caption = "Single Line"
    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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71