RichTextBox.GetLineFromCharIndex(Int32) Metoda

Definice

Načte číslo řádku ze zadané pozice znaku v textu RichTextBox ovládacího prvku.

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

Pozice znakového indexu, která se má prohledávat.

Návraty

Číslo řádku od nuly, ve kterém je umístěn index znaků.

Příklady

Následující příklad kódu ukazuje použití GetLineFromCharIndex metody . Pokud chcete spustit příklad, vložte následující kód do formuláře obsahujícího RichTextBox ovládací prvek s názvem RichTextBox1, tlačítko s názvem Button1 a dvě textová pole s názvem TextBox1 a TextBox2. Když je příklad spuštěný, zadejte do pole hledaný řetězec TextBox2 a kliknutím na tlačítko získejte výsledky hledání.

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

Poznámky

Tato metoda umožňuje určit číslo řádku na základě indexu znaků zadaného index v parametru metody. První řádek textu v ovládacím prvku vrátí hodnotu nula. Metoda GetLineFromCharIndex vrátí číslo fyzického řádku, kde se indexovaný znak nachází v ovládacím prvku. Pokud se například část prvního logického řádku textu v ovládacím prvku zalomí na další řádek, GetLineFromCharIndex vrátí metoda hodnotu 1, pokud je znak v zadaném indexu znaků zalomený do druhého fyzického řádku. Pokud WordWrap je nastavena na falsehodnotu , žádná část řádku se nezalomí na další a metoda vrátí hodnotu 0 pro zadaný index znaků. Tuto metodu můžete použít k určení řádku, ve kterém se nachází určitý index znaků. Například po volání Find metody pro hledání textu můžete získat index znaků na místo, kde jsou nalezeny výsledky hledání. Tuto metodu můžete volat s indexem znaků vráceným metodou Find k určení řádku, který slovo bylo nalezeno.

V některých případech nevyvolá výjimku, GetLineFromCharIndex pokud index je parametr neplatná hodnota. Příklad:

V těchto případech před voláním GetLineFromCharIndexověřte vstup .

Poznámka

Pokud je index znaků zadaný v parametru index nad dostupný počet řádků obsažených v ovládacím prvku, vrátí se číslo posledního řádku.

Platí pro

Viz také