How to scroll to a specific line number

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
oldummy
Posts: 80
Joined: Mon Jun 13, 2016 3:21 pm

How to scroll to a specific line number

Post by oldummy » Fri May 03, 2024 1:45 pm

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.

Klaus
Posts: 13863
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to scroll to a specific line number

Post by Klaus » Fri May 03, 2024 2:07 pm

Please check this post: viewtopic.php?t=6054

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9731
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to scroll to a specific line number

Post by dunbarx » Fri May 03, 2024 2:14 pm

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

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1211
Joined: Thu Apr 11, 2013 11:27 am

Re: How to scroll to a specific line number

Post by LCMark » Fri May 03, 2024 4:40 pm

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.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4016
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: How to scroll to a specific line number

Post by bn » Fri May 03, 2024 4:42 pm

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

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1211
Joined: Thu Apr 11, 2013 11:27 am

Re: How to scroll to a specific line number

Post by LCMark » Fri May 03, 2024 4:43 pm

Heh @bn's is better - I forgot about adjusting for the top margin :D

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”