TextBoxBase.Select(Int32, Int32) 方法

定义

选择文本框中的文本范围。Selects a range of text in the text box.

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

文本框中当前选定文本的第一个字符的位置。The position of the first character in the current text selection within the text box.

length
Int32

要选择的字符数。The number of characters to select.

异常

start 参数的值小于零。The value of the start parameter is less than zero.

示例

下面的代码示例使用TextBox派生类,在控件的内容中搜索单词 "fox" 的实例。The following code example uses TextBox, a derived class, to search the contents of the control for the instance of the word "fox". 如果找到,则代码使用Select方法选择控件中的单词。If found, the code selects the word in the control using the Select method. 此示例要求已创建TextBoxtextBox1一个名为的, Text其属性包含一个 "快速棕色 fox 跨延迟 dog" 的句子。This example requires that a TextBox named textBox1 has been created and its Text property contains the sentence "The quick brown fox jumps over the lazy dog."

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。If you want to set the start position to the first character in the control's text, set the start parameter to 0. 您可以使用此方法来选择文本的子字符串,例如搜索控件的文本和替换信息。You can use this method to select a substring of text, such as when searching through the text of the control and replacing information.

备注

您可以通过将start参数设置为文本框中您希望插入符号移动到的位置,并length将参数设置为零(0)值,以编程方式在文本框中移动插入符号。You can programmatically move the caret within the text box by setting the start parameter to the position within the text box where you want the caret to move to and set the length parameter to a value of zero (0). 文本框必须具有焦点,才能移动插入符号。The text box must have focus in order for the caret to be moved.

备注

如果在没有任何参数的情况下调用此方法,将使用替代方法。If this method is called without any parameters, an alternative method is used. 此替代方法从Control类继承。This alternative method inherits from the Control class. 调用时,它会将输入焦点设置到控件,并选择控件的内容。When called, it sets the input focus to the control and selects the contents of the control. 有关更多信息,请参见 Control.Select 方法。For more information, see the Control.Select method.

适用于

另请参阅