TextBox コントロール、SetFocus メソッド、EnterFieldBehavior プロパティ、HideSelection プロパティ、MultiLine プロパティ、Value プロパティの例

次の例では、1 つのフォームまたは複数のフォームのコンテキストで HideSelection プロパティを示します。

ユーザーは、 テキスト ボックス 内のテキストとタブをフォーム上の他のコントロールに選択したり、フォーカスを 2 番目のフォームに転送したりできます。 このコード サンプルでは 、SetFocus メソッドと、 EnterFieldBehaviorMultiLineValue プロパティも使用します。

この例を使用するには、以下の手順を行います。

  1. このサンプル コード (最後のイベント サブルーチンを除きます) をフォームの Declarations 部分にコピーします。

  2. TextBox1 という名前の大きな TextBoxToggleButton1 という名前の ToggleButton、 CommandButton1 という名前の CommandButton を追加します。

  3. UserForm2 という名前の第 2 のフォームをこのプロジェクトに挿入します。

  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 のサポートおよびフィードバックを参照してください。