通过


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 属性包含“快速棕色狐狸跳过懒惰的狗”一句。

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 方法。

适用于

另请参阅