RichTextBox.GetLineFromCharIndex(Int32) RichTextBox.GetLineFromCharIndex(Int32) RichTextBox.GetLineFromCharIndex(Int32) RichTextBox.GetLineFromCharIndex(Int32) Method

定义

RichTextBox 控件文本内的指定字符位置检索行号。Retrieves the line number from the specified character position within the text of the RichTextBox control.

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

参数

index
Int32 Int32 Int32 Int32

要搜索的字符索引位置。The character index position to search.

返回

字符索引所在的从零开始的行号。The zero-based line number in which the character index is located.

示例

下面的代码示例演示如何使用GetLineFromCharIndex方法。The following code example demonstrates using the GetLineFromCharIndex method. 若要运行该示例,请将以下代码粘贴到一个包含RichTextBox名为RichTextBox1的控件的窗Button1体中,并将TextBox1一个TextBox2名为的按钮和两个名为的文本框。To run the example, paste the following code in a form containing a RichTextBox control named RichTextBox1, a button named Button1 and two text boxes named TextBox1 and TextBox2. 当示例运行时,在中TextBox2输入搜索字符串,并单击按钮获取搜索结果。When the example is running, enter a search string in TextBox2 and click the button to get search results.

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

注解

此方法使您能够根据在方法的index参数中指定的字符索引来确定行号。This method enables you to determine the line number based on the character index specified in the index parameter of the method. 控件中的第一行文本返回值零。The first line of text in the control returns the value zero. GetLineFromCharIndex方法返回索引字符位于控件内的物理行号。The GetLineFromCharIndex method returns the physical line number where the indexed character is located within the control. 例如,如果控件中第一个逻辑文本行的一部分被换到下一行,则当指定字符索引GetLineFromCharIndex处的字符已包装到第二个物理行时,该方法返回1。For example, if a portion of the first logical line of text in the control wraps to the next line, the GetLineFromCharIndex method returns 1 if the character at the specified character index has wrapped to the second physical line. 如果WordWrap将设置为false,则行的任何部分都不会换到下一步,并且方法将为指定的字符索引返回0。If WordWrap is set to false, no portion of the line wraps to the next, and the method returns 0 for the specified character index. 您可以使用此方法来确定特定字符索引所在的行。You can use this method to determine which line a specific character index is located within. 例如,在调用Find方法搜索文本后,您可以获取搜索结果所在位置的字符索引。For example, after calling the Find method to search for text, you can obtain the character index to where the search results are found. 您可以使用该Find方法返回的字符索引来调用此方法,以确定找到该单词的行。You can call this method with the character index returned by the Find method to determine which line the word was found.

在某些情况下GetLineFromCharIndexindex如果参数为无效值,则不会引发异常。In certain cases, GetLineFromCharIndex does not throw an exception when the index parameter is an invalid value. 例如:For example:

在这些情况下,请在调用GetLineFromCharIndex前验证输入。In these cases, validate the input before calling GetLineFromCharIndex.

备注

如果index参数中指定的字符索引超出了控件内包含的可用行数,则返回最后一个行号。If the character index specified in the index parameter is beyond the available number of lines contained within the control, the last line number is returned.

适用于

另请参阅