Share via


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 參數的值小於零。

範例

下列程式碼範例會使用 TextBox 衍生類別來搜尋 控制項的內容,以尋找 「fox」 一詞的實例。 如果找到,程式碼會使用 Select 方法選取 控制項中的單字。 此範例要求 TextBox 已建立具名 textBox1 ,且其 Text 屬性包含「快速黑色 fox 跳過延遲狗」句子。

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 參數設定為要移動插入號的文字方塊內的位置,並將參數設定 length 為零的值, (0) 。 文字方塊必須有焦點,才能移動插入號。

注意

如果沒有任何參數呼叫這個方法,則會使用替代方法。 這個替代方法繼承自 Control 類別。 呼叫時,它會將輸入焦點設定為控制項,並選取控制項的內容。 如需詳細資訊,請參閱 Control.Select 方法。

適用於

另請參閱