TextBox 控件、SetFocus 方法、EnterFieldBehavior、HideSelection、MultiLine 和 Value 属性示例

以下示例演示了单个窗体或多个窗体的上下文中的 HideSelection 属性。

用户可以在 TextBox 和选项卡中选择文本以转到窗体上的其他控件,以及将焦点转移到第二个窗体。 此代码示例还使用 SetFocus 方法以及 EnterFieldBehaviorMultiLineValue 属性。

若要使用此示例,请按照下列步骤操作:

  1. 将此示例代码(最后的事件子例程除外)复制到窗体的 Declarations 部分。

  2. 添加名为 TextBox1 的大型 TextBox 、名为 ToggleButton1 的 ToggleButton 和名为 CommandButton1 的 CommandButton。

  3. 将第二个窗体插入此名为 UserForm2 的项目。

  4. 将此列表中最后的事件子例程粘贴到 UserForm2 的 Declarations 部分。

  5. 在此窗体中,添加一个名为 CommandButton1 的 CommandButton

  6. 运行 UserForm1。

' ***** Code for UserForm1 ***** 
Private Sub CommandButton1_Click() 
 TextBox1.SetFocus 
 UserForm2.Show 'Bring up the second form. 
End Sub
Private Sub ToggleButton1_Click() 
 If ToggleButton1.Value = True Then 
 TextBox1.HideSelection = False 
 ToggleButton1.Caption = "Selection Visible" 
 Else 
 TextBox1.HideSelection = True 
 ToggleButton1.Caption = "Selection Hidden" 
 End If 
End Sub
Private Sub UserForm_Initialize() 
 TextBox1.MultiLine = True 
 TextBox1.EnterFieldBehavior = fmEnterFieldBehaviorRecallSelection 
 
'Fill the TextBox 
 TextBox1.Text = "SelText indicates the starting " _ 
 & "point of selected text, or the insertion " _ 
 & point if no text is selected." & Chr$(10) _ 
 & Chr$(13) & "The SelStart property is " _ 
 & "always valid, even when the control does " _ 
 & "not have focus. Setting SelStart to a " _ 
 & "value less than zero creates an error. " _ 
 & Chr$(10) & Chr$(13) & "Changing the value " _ 
 & "of SelStart cancels any existing " _ 
 & "selection in the control, places " _ 
 & "an insertion point in the text, and sets " _ 
 & "the SelLength property to zero." 
 
 TextBox1.HideSelection = True 
 ToggleButton1.Caption = "Selection Hidden" 
 ToggleButton1.Value = False 

End Sub
'
' ***** Code for UserForm2 *****

Private Sub CommandButton1_Click() 
 UserForm2.Hide 
End Sub

支持和反馈

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