Udostępnij za pośrednictwem


RichTextBox.GetLineFromCharIndex(Int32) Metoda

Definicja

Pobiera numer wiersza z określonej pozycji 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 zerowego, w którym znajduje się indeks znaków.

Przykłady

W poniższym przykładzie GetLineFromCharIndex kodu pokazano użycie metody . Aby uruchomić przykład, wklej następujący kod w formularzu zawierającym kontrolkę RichTextBox o nazwie , przycisk o nazwie RichTextBox1i dwa pola tekstowe o nazwie TextBox1Button1 i TextBox2. Po uruchomieniu przykładu wprowadź ciąg wyszukiwania w TextBox2 pliku i 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 index w parametrze metody . Pierwszy wiersz tekstu w kontrolce zwraca wartość zero. Metoda GetLineFromCharIndex zwraca fizyczny numer wiersza, w którym znajduje się indeksowany znak 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 nie 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 metody w celu wyszukania Find tekstu można uzyskać indeks znaków, do którego można znaleźć wyniki wyszukiwania. Tę metodę można wywołać za pomocą indeksu znaków zwróconego Find przez metodę , aby określić, który wiersz został znaleziony.

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

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

Uwaga / Notatka

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

Dotyczy

Zobacz też