blah blah blah is here! blah blah » Close

up1down
link

I need to get the number of lines in a richtextbox relative to the richtextbox's width not the number of newline characters. Wordwrap is enabled in the richtextbox and I want to count all of the visible lines, not just the actual lines (made by pressing enter). How could I do this? I apologize if my question is not clear.

last answered 2 years ago

1 answers

link

The following code should count all the 'physical' lines in a richtextbox:

int lines;
if (richTextBox1.TextLength > 0)
{
lines = richTextBox1.GetLineFromCharIndex(richTextBox1.TextLength - 1) + 1;
}
else
{
lines = 0;
}
MessageBox.Show(String.Format("Number of lines is {0}", lines));

If the last or only line in the richtextbox is empty, then the code will not count it as a line.

Feedback