Lines in a TextArea?

Is it possible to determine how many lines of text are in a populated text area?
I have looked in the LR and cannot see anything relevant.

Thank you all in advance.

Please see if this is of any help.

Thanks Syed, I will look into that.

[quote=146677:@Richard Summers]Hi,
Is it possible to determine how many lines of text are in a populated text area?
I have looked in the LR and cannot see anything relevant.

Text in a TextArea wraps, so there is no direct way to know the resulting number of lines, since you cannot count the number of EndOfLIne.

One way is to use a picture to measure TextHeight for a few characters with the same font, then use StringHeight for the entire Text property with the same width as the TextArea, and divide by the one liner height.

Reviving a necro-topic, but, I was searching for this answer for Java and google gave me Xojo. Never used it, but trialled realbasic a while back.

Anyway, I remember doing this ages ago on Realbasic and (on Windows at least) you do this:

Dim eol_ascii, number_of_lines as integer
eol_ascii = asc(left(EndofLine.Windows,1))
number_of_lines = CountFields(TextArea1.Text,chr(eol_asc))

This will return the number of lines delimited by a or depending on your platform, regardless of whether they wrap or not in the textarea.

Of course, it might be totally different now with xoko so YMMV.

What is this for ?

Not tested (I debate about that: it may that I forgot), but if you pass the CharPos of the last character, you will get its line number.

If you add a timer, compute that in the timer, you will always get - at a moment in Time - the line number for the last character i the TextEdit (father of TextField and TextArea).

I recently wanted to vertically center the cursor in a TextArea and check (or not) that.

That’s the right thougt Emile, I posted it in another conversation, now here is it again:

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: