TextBoxBase.Select(Int32, Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在文字框中選取一段文字範圍。
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 屬性包含句子「快速的棕色狐狸跳過懶惰的狗」。
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 方法。