Get number of lines in a textarea

Hi everybody

If i want to get the total number of lines in some text (textarea) how will be the code for it?

Thanks for helping


something like :
numberOfLines = CountField( myTextArea.text, endofline)

Dim lineNum As Integer
lineNum = TextArea1.LineNumAtCharPos(TextArea1.Text.Len) + 1

The difference between those two answers is a line of text, as defined by endofline, might be displayed, or wrapped, as multiple lines in the text area. Jean-Yves gives the number of lines of text, regardless of how they are displayed. Marco gives the number of lines displayed in the textarea.

Hi Guys

Many many thanks for your help


With EndOfLine you get the number of paragraphs, not the number of lines. Quite possible that Alan meant paragraphs when he said lines, but in any case he has answers for both.

Had the same problem, the Linebreak is not countable with EndOfLine or other Chars (Char(13), …) because the Line/Word-Wrap of the TextArea is not a Linebreak.

Here’s my solution to get the number of lines at a “word-wrapped” TextArea.

//TextArea is called StyledTextArea

Dim intCharCount As Integer //Amount of Characters
Dim intLineCount As Integer // Amount of Lines
Dim intLoop As Integer // for working with every line

//Select whole Text from TextArea to count selected Characters

//Get the Count from the selected Text
intCharCount = StyledTextArea.SelLength

//Get the number of the Last Line from TextArea
intLineCount = StyledTextArea.LineNumAtCharPos(intCharCount)

MsgBox "Number of Lines: " + Cstr(intLineCount) + 1 // because counts from 0

//now you can work with every line, read styles, print lines and styles, ...
For intLoop = 0 to intLineCount
// here comes your routine for every line

Hope that helps :wink:


1 Like