RichTextBox.GetLineFromCharIndex Discrepancy

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can someone please explain why I get a different result when using the
GetLineFromCharIndex method on the last char in the Text property and the
Lines.Length property?

MainForm_gedcomTextBox is a RichTextBox.

For example this code:

private void End_menuItem_Click(object sender, System.EventArgs e)
{
int lastLine = MainForm_gedcomTextBox.GetLineFromCharIndex
(MainForm_gedcomTextBox.Text.Length -1);
string one =
MainForm_gedcomTextBox.Text.Substring(MainForm_gedcomTextBox.Text.Length-6);
string two =
MainForm_gedcomTextBox.Lines[MainForm_gedcomTextBox.Lines.Length-1];
MainForm_gedcomTextBox.SelectLine(MainForm_gedcomTextBox.Lines.Length-1);
}

produced:
lastLine = 144352
one = "0 TRLR"
two = "0 TRLR"
MainForm_gedcomTextBox.Lines.Length = 144082
 
Hi Arlyn_L,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that the GetLineFromCharIndex returns
different line number of the last character from the Lines.Length-1. If
there is any misunderstanding, please feel free to let me know.

Based on the document, we can see that GetLineFromCharIndex 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.

However, the Lines property considers lines as logical lines where each
line ends with a carrige return. So there will be difference between two
return values of line number, while the strings are the same.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top