Share via


TextBox 控制項、SetFocus 方法、EnterFieldBehavior、HideSelection、MultiLine、Value 屬性範例

下列範例示範單一表單或多個表單內容中的 HideSelection 屬性。

使用者可以選取 TextBox 中的文字和表單上其他控制項的索引標籤,以及將焦點傳輸到第二個表單。 此程式碼範例也會使用 SetFocus 方法,以及 EnterFieldBehaviorMultiLineValue 屬性。

若要使用此範例,請遵循下列步驟:

  1. 將此範例程式碼複製 (,但最後一個事件副程式) 至表單的宣告部分除外。

  2. 新增名為 TextBox1 的大型 TextBox 、名為 ToggleButton1 的 ToggleButton ,以及名為 CommandButton1 的 CommandButton

  3. 在此名為 UserForm2 的專案中插入第二個表單。

  4. 將此清單的最後一個事件副程式貼到 UserForm2 的 [宣告] 區段中。

  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 支援與意見反應