RichTextBox.GetLineFromCharIndex(Int32) Metodo

Definizione

Recupera il numero di riga dalla posizione del carattere specificata all'interno del testo del controllo RichTextBox.

public:
 int GetLineFromCharIndex(int index);
public:
 override int GetLineFromCharIndex(int index);
public int GetLineFromCharIndex (int index);
public override int GetLineFromCharIndex (int index);
member this.GetLineFromCharIndex : int -> int
override this.GetLineFromCharIndex : int -> int
Public Function GetLineFromCharIndex (index As Integer) As Integer
Public Overrides Function GetLineFromCharIndex (index As Integer) As Integer

Parametri

index
Int32

Posizione dell'indice del carattere da ricercare.

Restituisce

Numero di riga in base zero in cui si trova l'indice del carattere.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso del GetLineFromCharIndex metodo . Per eseguire l'esempio, incollare il codice seguente in un modulo contenente un RichTextBox controllo denominato , un pulsante denominato Button1RichTextBox1e due caselle di testo denominate TextBox1 e TextBox2. Quando l'esempio è in esecuzione, immettere una stringa di ricerca in TextBox2 e fare clic sul pulsante per ottenere i risultati della ricerca.

// This method demonstrates retrieving line numbers that 
// indicate the location of a particular word
// contained in a RichTextBox. The line numbers are zero-based.
void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   
   // Reset the results box.
   TextBox1->Text = "";
   
   // Get the word to search from from TextBox2.
   String^ searchWord = TextBox2->Text;
   int index = 0;
   
   //Declare an ArrayList to store line numbers.
   System::Collections::ArrayList^ lineList = gcnew System::Collections::ArrayList;
   do
   {
      // Find occurrences of the search word, incrementing  
      // the start index. 
      index = RichTextBox1->Find( searchWord, index + 1, RichTextBoxFinds::MatchCase );
      if ( index != -1 )
      {
         lineList->Add( RichTextBox1->GetLineFromCharIndex( index ) );
      }
   }
   while ( (index != -1) );

   // Iterate through the list and display the line numbers in TextBox1.
   System::Collections::IEnumerator^ myEnumerator = lineList->GetEnumerator();
   if ( lineList->Count <= 0 )
   {
      TextBox1->Text = searchWord + " was not found";
   }
   else
   {
      TextBox1->SelectedText = searchWord + " was found on line(s):";
      while ( myEnumerator->MoveNext() )
      {
         TextBox1->SelectedText = myEnumerator->Current + " ";
      }
   }
}
// This method demonstrates retrieving line numbers that 
// indicate the location of a particular word
// contained in a RichTextBox. The line numbers are zero-based.

private void Button1_Click(System.Object sender, System.EventArgs e)
{

    // Reset the results box.
    TextBox1.Text = "";

    // Get the word to search from from TextBox2.
    string searchWord = TextBox2.Text;

    int index = 0;

    //Declare an ArrayList to store line numbers.
    System.Collections.ArrayList lineList = new System.Collections.ArrayList();
    do
    {
        // Find occurrences of the search word, incrementing  
        // the start index. 
        index = RichTextBox1.Find(searchWord, index+1, RichTextBoxFinds.MatchCase);
        if (index!=-1)

            // Find the word's line number and add the line 
            // number to the arrayList. 
        {
            lineList.Add(RichTextBox1.GetLineFromCharIndex(index));
        }
    }
    while((index!=-1));

    // Iterate through the list and display the line numbers in TextBox1.
    System.Collections.IEnumerator myEnumerator = lineList.GetEnumerator();
    if (lineList.Count<=0)
    {
        TextBox1.Text = searchWord+" was not found";
    }
    else
    {
        TextBox1.SelectedText = searchWord+" was found on line(s):";
        while (myEnumerator.MoveNext())
        {
            TextBox1.SelectedText = myEnumerator.Current+" ";
        }
    }
}
' This method demonstrates retrieving line numbers that 
' indicate the location of a particular word
' contained in a RichTextBox. The line numbers are zero-based.

Private Sub Button1_Click(ByVal sender As System.Object, _ 
    ByVal e As System.EventArgs) Handles Button1.Click

    ' Reset the results box.
    TextBox1.Text = ""

    ' Get the word to search from from TextBox2.
    Dim searchWord As String = TextBox2.Text

    Dim index As Integer = 0

    'Declare an ArrayList to store line numbers.
    Dim lineList As New System.Collections.ArrayList
    Do
        ' Find occurrences of the search word, incrementing  
        ' the start index. 
        index = RichTextBox1.Find(searchWord, index + 1, _
            RichTextBoxFinds.MatchCase)
        If (index <> -1) Then

            ' Find the word's line number and add the line 
            'number to the arrayList. 
            lineList.Add(RichTextBox1.GetLineFromCharIndex(index))
        End If
    Loop While (index <> -1)

    ' Iterate through the list and display the line numbers in TextBox1.
    Dim myEnumerator As System.Collections.IEnumerator = _
        lineList.GetEnumerator()
    If lineList.Count <= 0 Then
        TextBox1.Text = searchWord & " was not found"
    Else
        TextBox1.SelectedText = searchWord & " was found on line(s):"
        While (myEnumerator.MoveNext)
            TextBox1.SelectedText = myEnumerator.Current & " "
        End While
    End If

End Sub

Commenti

Questo metodo consente di determinare il numero di riga in base all'indice di caratteri specificato nel index parametro del metodo . La prima riga di testo nel controllo restituisce il valore zero. Il GetLineFromCharIndex metodo restituisce il numero di riga fisica in cui si trova il carattere indicizzato all'interno del controllo . Se, ad esempio, una parte della prima riga logica del testo nel controllo esegue il wrapping alla riga successiva, il GetLineFromCharIndex metodo restituisce 1 se il carattere in corrispondenza dell'indice di caratteri specificato è stato sottoposto a wrapping nella seconda riga fisica. Se WordWrap è impostato su false, nessuna parte della riga esegue il wrapping successivo e il metodo restituisce 0 per l'indice di caratteri specificato. È possibile utilizzare questo metodo per determinare la riga in cui si trova un indice di caratteri specifico. Ad esempio, dopo aver chiamato il Find metodo per cercare testo, è possibile ottenere l'indice dei caratteri in cui vengono trovati i risultati della ricerca. È possibile chiamare questo metodo con l'indice di caratteri restituito dal Find metodo per determinare quale riga è stata trovata.

In alcuni casi, GetLineFromCharIndex non genera un'eccezione quando il index parametro è un valore non valido. Ad esempio:

In questi casi, convalidare l'input prima di chiamare GetLineFromCharIndex.

Nota

Se l'indice di caratteri specificato nel index parametro supera il numero di righe disponibile contenuto nel controllo, viene restituito l'ultimo numero di riga.

Si applica a

Vedi anche