RichTextBox.GetLineFromCharIndex(Int32) Metoda

Definicja

Pobiera numer wiersza z określonego położenia znaku w tekście kontrolki 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

Parametry

index
Int32

Pozycja indeksu znaków do wyszukania.

Zwraca

Numer wiersza zero, w którym znajduje się indeks znaków.

Przykłady

Poniższy przykład kodu pokazuje użycie GetLineFromCharIndex metody . Aby uruchomić przykład, wklej następujący kod w formularzu zawierającym kontrolkę RichTextBox o nazwie RichTextBox1, przycisk o nazwie i dwa pola tekstowe o nazwie TextBox1Button1 i TextBox2. Po uruchomieniu przykładu wprowadź ciąg wyszukiwania i TextBox2 kliknij przycisk, aby uzyskać wyniki wyszukiwania.

// 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

Uwagi

Ta metoda umożliwia określenie numeru wiersza na podstawie indeksu znaków określonego w index parametrze metody. Pierwszy wiersz tekstu w kontrolce zwraca wartość zero. Metoda GetLineFromCharIndex zwraca fizyczny numer wiersza, w którym indeksowany znak znajduje się w kontrolce. Jeśli na przykład część pierwszego logicznego wiersza tekstu w kontrolce zawija się do następnego wiersza, metoda zwraca wartość 1, GetLineFromCharIndex jeśli znak w określonym indeksie znaków został opakowany do drugiego wiersza fizycznego. Jeśli WordWrap ustawiono wartość false, żadna część wiersza zawija się do następnego, a metoda zwraca wartość 0 dla określonego indeksu znaków. Za pomocą tej metody można określić, w którym wierszu znajduje się określony indeks znaków. Na przykład po wywołaniu Find metody wyszukiwania tekstu można uzyskać indeks znaków, do którego znajdują się wyniki wyszukiwania. Tę metodę można wywołać za pomocą indeksu znaków zwróconego przez Find metodę, aby określić, który wiersz został znaleziony.

W niektórych przypadkach nie zgłasza wyjątkuindex, GetLineFromCharIndex gdy parametr jest nieprawidłową wartością. Przykład:

  • index Jeśli parametr ma wartość MinValue lub -1, GetLineFromCharIndex zwraca wartość 0.

  • index Jeśli parametr jest długością tekstu lub MaxValueGetLineFromCharIndex , zwraca liczbę ostatniego wiersza tekstu, który niekoniecznie jest taki sam jak Lines.Length-1, w zależności od wartości WordWrap właściwości.

W takich przypadkach zweryfikuj dane wejściowe przed wywołaniem metody GetLineFromCharIndex.

Uwaga

Jeśli indeks znaków określony w parametrze index wykracza poza dostępną liczbę wierszy zawartych w kontrolce, zwracany jest ostatni numer wiersza.

Dotyczy

Zobacz też