Page 1 of 1

textarea line count

Posted: Wednesday 22nd December 2021 4:32pm
by bill-lancaster
I'd like to adjust the height of a textarea (with Wrap = TRUE) so that all the text is displayed.

Knowing the number of lines displayed would be a help.

Any ideas?

Re: textarea line count

Posted: Wednesday 22nd December 2021 4:56pm
by BruceSteers
bill-lancaster wrote: Wednesday 22nd December 2021 4:32pm I'd like to adjust the height of a textarea (with Wrap = TRUE) so that all the text is displayed.

Knowing the number of lines displayed would be a help.

Any ideas?
the height should be..
TextArea1.Font.RichTextHeight(TextArea1.Text, TextArea1.Width) 
But add an extra bit (line height) for the border/margin..
TextArea1.Height = TextArea1.Font.RichTextHeight(TextArea1.Text, TextArea1.Width) + TextArea1.Font.Height
or to specifically answer your question on how to get the visible lines then..
 Dim iLines as Integer = TextArea1.Font.RichTextHeight(TextArea1.Text, TextArea1.Width) / TextArea1.Font.Height

Re: textarea line count

Posted: Wednesday 22nd December 2021 5:26pm
by BruceSteers
Does it have to be a TextArea?
Trouble with using RichTextHeight is TextArea is not RichRext

A TextLabel is RichText and has AutoResize and will give better results for RichtextHeight than a TextArea will

Re: textarea line count

Posted: Wednesday 22nd December 2021 5:33pm
by bill-lancaster
Thanks Bruce that works very well.
I want the displayed text to be editable so I think I'm stuck with textarea.
Bill

Re: textarea line count

Posted: Monday 31st January 2022 4:31am
by echo8hink
I find total lines in a textarea control with two steps:

Code: Select all

 Dim totalLines As Integer
  'move cursor to last position (the end) of text area
  TextArea1.CursorAt(TextArea1.Length)
  'get total lines from last line number in text area
  totalLines = TextArea1.Line
With autowrap turned on, you may have to hunt the text for linefeed or carriage return codes to come up with a better number because a line that is auto-wrapped will appear on-screen as multiple lines, but the line count from the code will think both are the same line. In other words, a long line 20 that is wrapped will return line 20 on both "lines" of the display. Also, the line counting starts with line 0, if I remember correctly.

I use a version of this to paginate a printout on a little text editor app. Hope it helps.
-hink