Retrieves the line number from the specified character position within the text of the System.Windows.Forms.RichTextBox control.
The zero-based line number in which the character index is located.
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 RichTextBox.GetLineFromCharIndex(int) 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 RichTextBox.GetLineFromCharIndex(int) method returns 1 if the character at the specified character index has wrapped to the second physical line. If TextBoxBase.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 RichTextBox.Find(string) 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 RichTextBox.Find(string) method to determine which line the word was found.
In certain cases, RichTextBox.GetLineFromCharIndex(int) does not throw an exception when the index parameter is an invalid value. For example:
If the index parameter is int.MinValue or -1, RichTextBox.GetLineFromCharIndex(int) returns 0.
If the index parameter is the text length or int.MaxValue, RichTextBox.GetLineFromCharIndex(int) returns the number of the last line of text, which is not necessarily the same as Lines.Length-1, depending on the value of the TextBoxBase.WordWrap property.
In these cases, validate the input before calling RichTextBox.GetLineFromCharIndex(int).
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.