Page 1 of 1

How to scroll to a specific line number

Posted: Fri May 03, 2024 1:45 pm
by oldummy
I have a scrolling field with 100+ lines of text in it. I would like to enter a line number into a separate field and have the scroll set to that particular line of the field containing. If I were looking for specific text I could use search, but since the line number I need to scroll to will vary, well here I am again.

Re: How to scroll to a specific line number

Posted: Fri May 03, 2024 2:07 pm
by Klaus
Please check this post: viewtopic.php?t=6054

Re: How to scroll to a specific line number

Posted: Fri May 03, 2024 2:14 pm
by dunbarx
Make a field with many lines of text. In a button somewhere, put:

Code: Select all

on mouseUp
   set the fixedLineheight of fld 1 to "true"
   ask "What Line?"
   set the scroll of fld 1 to it * the textheight of fld 1 - the textHeight of fld 1
end mouseUp
Craig

Re: How to scroll to a specific line number

Posted: Fri May 03, 2024 4:40 pm
by LCMark
There's a more general way to do this which works with lines without fixed height...

You can use the formattedRect property to get the rect of a line of a field in card co-ordinates as it would be if the field didn't clip the content, which you can then adjust by the top of the field and current vScroll to get a new scroll value which puts the line at the top of field:

Code: Select all

   /* Get the rect (in card co-ords) of the line of text. */
   local tLineRect
   put the formattedRect of line tLineNumber of field 1 into tLineRect
   
   /* Get the top of the target field relative to the current scroll
    * (the adjustment is necessary as the scroll affects the formatted
    * rect of the lines in the obvious way). */
   local tFieldTop
   put the top of field 1 - the vScroll of field 1 into tFieldTop
   
   /* The scroll is the difference between the top of the field
    * and the top of the line (item 2 of the rect). */
   set the vScroll of field 1 to item 2 of tLineRect - tFieldTop
 
This should work with field regardless of the height of each line, whether due to wrapping or due to styling of the text in the line.

Re: How to scroll to a specific line number

Posted: Fri May 03, 2024 4:42 pm
by bn
Make a button and a field that holds the line number you want to scroll to.

Code: Select all

on mouseUp
   ## a field that holds the line number you want to scroll to
   put field "fLineNum" into tLineNum
   
   ## the field that holds your text
   put the height of field "fText" into tFieldHeight
   put the top of field "fText" into tFieldTop
   put the topMargin of field "fText" into tTopMargin
   put the vScroll of field "fText" into tvScroll
   
   put the formattedTop of line tLineNum of field "fText" into tFormatTop
   
   put tFormatTop - tFieldTop into tAbsDiff
   
   put tAbsDiff + tvScroll - tTopMargin into tNewScroll
   set the vScroll of field "fText" to tNewScroll
   
   ## check the correct line
   select line tLineNum of field "fText"
end mouseUp
This does not need a fixed lineHeight for it to work.

Kind regards
Bernd

Re: How to scroll to a specific line number

Posted: Fri May 03, 2024 4:43 pm
by LCMark
Heh @bn's is better - I forgot about adjusting for the top margin :D