增强文本框控件的边框样式、颜色和特殊效果

以下示例演示 BorderColorSpecialEffect 属性,并显示通过这些属性可用的每个边框。 该示例还演示了如何使用 BackColorBackStyleBorderColorForecolor 属性控制颜色设置。

若要使用本示例,请将此示例代码复制到窗体的"脚本编辑器"中。 若要运行本代码,需要打开该窗体,以便激活 Open 事件。 确保该窗体包含:

  • 六个名称分别为"TextBox1"到"TextBox6"的 TextBox 控件。

  • 两个名称分别为"ToggleButton1"和"ToggleButton2"的 ToggleButton 控件。

Dim TextBox1 
Dim TextBox2 
Dim TextBox3 
Dim TextBox4 
Dim TextBox5 
Dim TextBox6 
Dim ToggleButton1 
Dim ToggleButton2 
 
Sub Item_Open() 
Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").TextBox1 
Set TextBox2 = Item.GetInspector.ModifiedFormPages("P.2").TextBox2 
Set TextBox3 = Item.GetInspector.ModifiedFormPages("P.2").TextBox3 
Set TextBox4 = Item.GetInspector.ModifiedFormPages("P.2").TextBox4 
Set TextBox5 = Item.GetInspector.ModifiedFormPages("P.2").TextBox5 
Set TextBox6 = Item.GetInspector.ModifiedFormPages("P.2").TextBox6 
Set ToggleButton1 = Item.GetInspector.ModifiedFormPages("P.2").ToggleButton1 
Set ToggleButton2 = Item.GetInspector.ModifiedFormPages("P.2").ToggleButton2 
'Initialize each TextBox with a border style or special effect, 
'and foreground and background colors 
 
'TextBox1 initially uses a borderstyle 
TextBox1.Text = "BorderStyle-Single" 
TextBox1.BorderStyle = 1 
TextBox1.BorderColor = RGB(255, 128, 128) 'Color - Salmon 
TextBox1.ForeColor = RGB(255, 255, 0) 'Color - Yellow 
TextBox1.BackColor = RGB(0, 128, 64) 'Color - Green #2 
 
'TextBoxes 2 through 6 initially use special effects 
TextBox2.Text = "Flat" 
TextBox2.SpecialEffect = 0 
TextBox2.ForeColor = RGB(64, 0, 0) 'Color - Brown 
TextBox2.BackColor = RGB(0, 0, 255) 'Color - Blue 
 
'Ensure the background style for TextBox2 is initially opaque. 
TextBox2.BackStyle = 1 
 
TextBox3.Text = "Etched" 
TextBox3.SpecialEffect = 3 
TextBox3.ForeColor = RGB(128, 0, 255) 'Color - Purple 
TextBox3.BackColor = RGB(0, 255, 255) 'Color - Cyan 
 
'Define BorderColor for later use (when borderstyle=fmBorderStyleSingle) 
TextBox3.BorderColor = RGB(0, 0, 0) 'Color - Black 
 
TextBox4.Text = "Bump" 
TextBox4.SpecialEffect = 6 
TextBox4.ForeColor = RGB(255, 0, 255) 'Color - Magenta 
TextBox4.BackColor = RGB(0, 0, 100) 'Color - Navy blue 
 
TextBox5.Text = "Raised" 
TextBox5.SpecialEffect = 1 
TextBox5.ForeColor = RGB(255, 0, 0) 'Color - Red 
TextBox5.BackColor = RGB(128, 128, 128) 'Color - Gray 
 
TextBox6.Text = "Sunken" 
TextBox6.SpecialEffect = 2 
TextBox6.ForeColor = RGB(0, 64, 0) 'Color - Olive 
TextBox6.BackColor = RGB(0, 255, 0) 'Color - Green #1 
 
ToggleButton1.Caption = "Swap styles" 
ToggleButton2.Caption = "Transparent/Opaque background" 
End Sub 
 
Sub ToggleButton1_Click() 
 
'Swap borders between TextBox1 and TextBox3 
If ToggleButton1.Value = True Then 
 'Change TextBox1 from BorderStyle to Etched 
 TextBox1.Text = "Etched" 
 TextBox1.SpecialEffect = 3 
 
 'Change TextBox3 from Etched to BorderStyle 
 TextBox3.Text = "BorderStyle-Single" 
 TextBox3.BorderStyle = 1 
Else 
 'Change TextBox1 back to BorderStyle 
 TextBox1.Text = "BorderStyle-Single" 
 TextBox1.BorderStyle = 1 
 
 'Change TextBox3 back to Etched 
 TextBox3.Text = "Etched" 
 TextBox3.SpecialEffect = 3 
End If 
End Sub 
 
 
Sub ToggleButton2_Click() 
 
'Set background to Opaque or Transparent 
If ToggleButton2.Value = True Then 
 'Change TextBox2 to a transparent background 
 TextBox2.BackStyle = 0 
Else 
 'Change TextBox2 back to opaque background 
 TextBox2.BackStyle = 1 
End If 
 
End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。