TextBoxBase.Select(Int32, Int32) Metod

Definition

Markerar ett textintervall i textrutan.

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)

Parametrar

start
Int32

Positionen för det första tecknet i den aktuella textmarkeringen i textrutan.

length
Int32

Antalet tecken som ska väljas.

Undantag

Värdet för parametern start är mindre än noll.

Exempel

I följande kodexempel används TextBox, en härledd klass, för att söka i innehållet i kontrollen efter instansen av ordet "fox". Om det hittas väljer koden ordet i kontrollen med hjälp av Select -metoden. Det här exemplet kräver att ett TextBox namngivet textBox1 namn har skapats och dess Text egenskap innehåller meningen "Den snabba bruna räven hoppar över den lata hunden".

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

Kommentarer

Om du vill ange startpositionen till det första tecknet i kontrollens text anger du parametern start till 0. Du kan använda den här metoden för att välja en delsträng med text, till exempel när du söker igenom texten i kontrollen och ersätter information.

Note

Du kan programmatiskt flytta caret i textrutan genom att ange parametern start till positionen i textrutan där du vill att caret ska flyttas till och ange parametern length till värdet noll (0). Textrutan måste ha fokus för att caret ska kunna flyttas.

Note

Om den här metoden anropas utan parametrar används en alternativ metod. Den här alternativa metoden ärver från Control klassen. När den anropas anger den indatafokus till kontrollen och väljer innehållet i kontrollen. Mer information finns i Control.Select-metoden.

Gäller för

Se även