RichTextBox.GetLineFromCharIndex Method
Gets the line number from the specified character position within the text of the RichTextBox control.
[Visual Basic] Public Function GetLineFromCharIndex( _ ByVal index As Integer _ ) As Integer [C#] public int GetLineFromCharIndex( int index ); [C++] public: int GetLineFromCharIndex( int index ); [JScript] public function GetLineFromCharIndex( index : int ) : int;
Parameters
- index
- The character index position to search.
Return Value
The zero-based line number where the character index is located in.
Remarks
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. The GetLineFromCharIndex method returns the physical line number where the indexed character is located within the control. 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. 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. For example, after calling the Find method to search for text, you can obtain the character index to where the search results are found. You can call this method with the character index returned by the Find method to determine which line the word was found.
Note 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.
Example
[Visual Basic, C#] The following code example demonstrates using the GetLineFromCharIndex method. 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. Ensure all events are connected with their event-handling methods. When the example is running, enter a search string in TextBox2 and click the button to get search results.
[Visual Basic] ' 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 [C#] // 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+" "; } } }
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
RichTextBox Class | RichTextBox Members | System.Windows.Forms Namespace | GetCharFromPosition | GetCharIndexFromPosition | GetPositionFromCharIndex