textarea line count

Post your Gambas programming questions here.
Post Reply
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

textarea line count

Post 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?
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: textarea line count

Post 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
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: textarea line count

Post 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
If at first you don't succeed , try doing something differently.
BruceS
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: textarea line count

Post 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
User avatar
echo8hink
Posts: 5
Joined: Friday 17th November 2017 5:39pm

Re: textarea line count

Post 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
Post Reply