TextBoxBase.Select(Int32, Int32) メソッド

定義

テキスト ボックスでテキストの範囲を選択します。

public:
 void Select(int start, int length);
public void Select (int start, int length);
override this.Select : int * int -> unit
Public Sub Select (start As Integer, length As Integer)

パラメーター

start
Int32

テキスト ボックス内で現在選択されているテキストの最初の文字の位置。

length
Int32

選択する文字数。

例外

start パラメーターの値が 0 未満です。

次のコード例では、派生クラスを使用 TextBoxして、"fox" という単語のインスタンスのコントロールの内容を検索します。 見つかった場合、コードはメソッドを使用してコントロール内の単語を Select 選択します。 この例では、 TextBox 名前が textBox1 作成され、そのプロパティに Text 「素早く茶色のキツネが怠惰な犬を飛び越える」という文が含まれている必要があります。

public:
   void SelectMyString()
   {
      // Create a string to search for the word "fox".
      String^ searchString = "fox";
      // Determine the starting location of the word "fox".
      int index = textBox1->Text->IndexOf( searchString, 16, 3 );
      // Determine if the word has been found and select it if it was.
      if ( index != -1 )
      {
         // Select the string using the index and the length of the string.
         textBox1->Select( index,searchString->Length );
      }
   }
public void SelectMyString()
 {
    // Create a string to search for the word "fox".
    String searchString = "fox";
    // Determine the starting location of the word "fox".
    int index = textBox1.Text.IndexOf(searchString, 16, 3);
    // Determine if the word has been found and select it if it was.
    if (index != -1)
    {
       // Select the string using the index and the length of the string.
       textBox1.Select(index, searchString.Length);
    }
 }
Public Sub SelectMyString()
    ' Create a string to search for the word "fox".
    Dim searchString As String = "fox"
    ' Determine the starting location of the word "fox".
    Dim index As Integer = textBox1.Text.IndexOf(searchString, 16, 3)
    ' Determine if the word has been found and select it if it was.
    If index <> - 1 Then
        ' Select the string using the index and the length of the string.
        textBox1.Select(index, searchString.Length)
    End If
End Sub

注釈

開始位置をコントロールのテキストの最初の文字に設定する場合は、パラメーターを start 0 に設定します。 このメソッドを使用すると、コントロールのテキストを検索したり、情報を置き換えたりする場合など、テキストの部分文字列を選択できます。

注意

キャレットを移動するテキスト ボックス内の位置にパラメーターを start 設定し、パラメーターをゼロ (0) に設定 length することで、テキスト ボックス内のキャレットをプログラムで移動できます。 キャレットを移動するには、テキスト ボックスにフォーカスが必要です。

注意

このメソッドがパラメーターなしで呼び出される場合は、別のメソッドが使用されます。 この代替メソッドは、クラスから Control 継承されます。 呼び出されると、入力フォーカスがコントロールに設定され、コントロールの内容が選択されます。 詳細については、Control.Select メソッドを参照してください。

適用対象

こちらもご覧ください