AutoSize、MultiLine、WordWrap 和 ScrollBars 属性、TextBox 控件示例
以下示例演示 TextBox 上的 MultiLine、WordWrap 和 ScrollBars 属性。
若要使用此示例,请将此示例代码复制到窗体的 Declarations 部分。 确保该窗体包含:
- 一个名为"TextBox1"的 TextBox 。
- 四个名称分别为"ToggleButton1"到"ToggleButton4"的 ToggleButton 控件。
若要查看放置在 TextBox 中的整个文本,请通过单击 ToggleButton 控件来将 MultiLine 和 WordWrap 设置为 True 。
当 MultiLine 为 True 时,可以通过按 Shift+Enter 输入新的文本行。 在手动更改 TextBox 的内容时,将显示 ScrollBars 。
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
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。