允许在文本框中多行、自动换行和滚动
以下示例演示 TextBox 上的 MultiLine、WordWrap 和 ScrollBars 属性。
若要使用本示例,请将此示例代码复制到窗体的"脚本编辑器"中。 若要运行本代码,需要打开该窗体,以便激活 Open 事件。 确保该窗体包含:
一个名为"TextBox1"的 TextBox 。
四个名称分别为"ToggleButton1"到"ToggleButton4"的 ToggleButton 控件。
若要查看放置在 TextBox 中的整个文本,请通过单击 ToggleButton 控件来将 MultiLine 和 WordWrap 设置为 True 。 当 MultiLine 为 True 时,可通过按 Shift+Enter 输入新的文本行。 在手动更改 TextBox 的内容时,将显示 ScrollBars 。
Dim ToggleButton1
Dim ToggleButton2
Dim ToggleButton3
Dim ToggleButton4
Dim TextBox1
Sub Item_Open
'Initialize TextBox properties and toggle buttons
Set ToggleButton1 = Item.GetInspector.ModifiedFormPages("P.2").ToggleButton1
Set ToggleButton2 = Item.GetInspector.ModifiedFormPages("P.2").ToggleButton2
Set ToggleButton3 = Item.GetInspector.ModifiedFormPages("P.2").ToggleButton3
Set ToggleButton4 = Item.GetInspector.ModifiedFormPages("P.2").ToggleButton4
Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").TextBox1
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
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
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
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
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 支持和反馈,获取有关如何接收支持和提供反馈的指南。
反馈
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:提交和查看相关反馈