This documentation is archived and is not being maintained.

RichTextBox.GetLineFromCharIndex Method

Retrieves the line number from the specified character position within the text of the RichTextBox control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public override int GetLineFromCharIndex (
	int index
)
public int GetLineFromCharIndex (
	int index
)
public override function GetLineFromCharIndex (
	index : int
) : int
Not applicable.

Parameters

index

The character index position to search.

Return Value

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

In certain cases, GetLineFromCharIndex does not throw an exception when the index parameter is an invalid value. For example:

  • If the index parameter is MinValue or -1, GetLineFromCharIndex returns 0.

  • If the index parameter is the text length or MaxValue, GetLineFromCharIndex 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 WordWrap property.

In these cases, validate the input before calling GetLineFromCharIndex.

NoteNote:

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.

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

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 void button1_Click(Object sender, System.EventArgs e)
{
    // Reset the results box.
    textBox1.set_Text("");
    // Get the word to search from from TextBox2.
    String searchWord = textBox2.get_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((System.Int32)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.get_Count() <= 0) {
        textBox1.set_Text(searchWord + " was not found");
    }
    else {
        textBox1.set_SelectedText(searchWord + " was found on line(s):");
        while (myEnumerator.MoveNext()) {
            textBox1.set_SelectedText(myEnumerator.get_Current() + " ");
        }
    }
} //button1_Click

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Show: