RichTextBox.GetLineFromCharIndex(Int32) Methode

Definitie

Hiermee wordt het regelnummer opgehaald uit de opgegeven tekenpositie in de tekst van het RichTextBox besturingselement.

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

Parameters

index
Int32

De positie van de tekenindex om te zoeken.

Retouren

Het regelnummer op basis van nul waarin de tekenindex zich bevindt.

Voorbeelden

In het volgende codevoorbeeld ziet u hoe u de GetLineFromCharIndex methode gebruikt. Als u het voorbeeld wilt uitvoeren, plakt u de volgende code in een formulier met een RichTextBox besturingselement met de naam RichTextBox1, een knop met de naam Button1 en twee tekstvakken met de naam TextBox1 en TextBox2. Wanneer het voorbeeld wordt uitgevoerd, voert u een zoekreeks in TextBox2 en klikt u op de knop om zoekresultaten op te halen.

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

Opmerkingen

Met deze methode kunt u het regelnummer bepalen op basis van de tekenindex die is opgegeven in de index parameter van de methode. De eerste regel tekst in het besturingselement retourneert de waarde nul. De GetLineFromCharIndex methode retourneert het fysieke regelnummer waar het geïndexeerde teken zich in het besturingselement bevindt. Als een deel van de eerste logische tekstregel in het besturingselement bijvoorbeeld terugloopt op de volgende regel, retourneert de GetLineFromCharIndex methode 1 als het teken op de opgegeven tekenindex is verpakt naar de tweede fysieke regel. Als WordWrap dit is ingesteld falseop, wordt geen deel van de regel teruggezet naar de volgende en retourneert de methode 0 voor de opgegeven tekenindex. U kunt deze methode gebruiken om te bepalen binnen welke regel een specifieke tekenindex zich bevindt. Nadat u bijvoorbeeld de Find methode hebt aangeroepen om naar tekst te zoeken, kunt u de tekenindex ophalen waar de zoekresultaten worden gevonden. U kunt deze methode aanroepen met de tekenindex die wordt geretourneerd door de Find methode om te bepalen welke regel het woord is gevonden.

In bepaalde gevallen GetLineFromCharIndex wordt er geen uitzondering gegenereerd wanneer de index parameter een ongeldige waarde is. Voorbeeld:

  • Als de index parameter is MinValue of -1, GetLineFromCharIndex retourneert u 0.

  • Als de index parameter de tekstlengte is of MaxValue, GetLineFromCharIndex wordt het nummer van de laatste tekstregel geretourneerd, wat niet noodzakelijkerwijs hetzelfde is als Lines.Length-1, afhankelijk van de waarde van de WordWrap eigenschap.

In deze gevallen valideert u de invoer voordat u aanroept GetLineFromCharIndex.

Note

Als de tekenindex die in de index parameter is opgegeven, buiten het beschikbare aantal regels in het besturingselement valt, wordt het laatste regelnummer geretourneerd.

Van toepassing op

Zie ook