Page 1 of 1

Line numbers in text box

Posted: Mon Mar 04, 2013 1:25 am
by Rod Edwardson
Hello

I am able to find the total number of lines in my text box but is there a way to have each line number displayed at the beginning of each line? Thanks in advance!

Rod

Re: Line numbers in text box

Posted: Mon Mar 04, 2013 1:38 am
by dunbarx
Hi.

No problem having line numbers precede each line, but this is not a native property.
Does this, maybe in a button script somewhere help to get you started?

Code: Select all

on mouseup
put field "yourField" into temp
repeat with y = 1 to the number of lines of temp
put y && line y of temp into line y of temp
end repeat
put temp into fld "yourField"
end mouseUp
Can you do this yourself? Do you see how it works? This also means managing the field contents when you add or subtract lines. Write back if that is a problem.

Craig Newman

Re: Line numbers in text box

Posted: Mon Mar 04, 2013 2:07 am
by Simon
To avoid manipulating your fld use 2 flds
Put this into your text fld:

Code: Select all

on scrollbarDrag newValue
  set the vScroll of fld  "Line Number" to newValue
end scrollbarDrag
The fld Line Number is set to the left of your text fld and full of... numbers

Now I have to figure out how to populate fld Line Numbers on the fly.

Simon
Oh, the text formatting has to be the same in both flds (font size and line height 'n stuff)

Re: Line numbers in text box

Posted: Mon Mar 04, 2013 2:21 am
by Simon
OK got it:

Code: Select all

on openCard
   put "" into fld "Line Number"
   repeat for the number of lines in fld "Text Field"
      add 1 to x
      put x & cr after fld "Line Number"
   end repeat
   delete the last char of fld "Line Number"
end openCard
That populates the Line Number field
and in the text fld:

Code: Select all

on scrollbarDrag newValue
  set the vScroll of fld  "Line Number" to newValue
end scrollbarDrag

on returnInField
 put the last line of fld "Line Number" into tLine
   add 1 to tLine
   put cr & tLine after fld "Line Number"
   pass returnInField
end returnInField

on enterInField
returnInField
end enterInField
There how's that :)

Simon
EDIT: RATS! only works if the dont wrap of the text fld is selected

Re: Line numbers in text box

Posted: Mon Mar 04, 2013 2:56 am
by Simon
Ok once more then I may shut up. :)

Code: Select all

on openCard
   put "" into fld "Line Number"
   put the formattedHeight of fld "Text Field" into tPix
   put round(tPix /16) into tPix --assumes a fixed line height of 16
   repeat with x = 1 to tPix
      put x & cr after fld "Line Number"
   end repeat
   delete the last char of fld "Line Number"
end openCard
Now you can have either don't wrap selected or not.

Simon
Now.... fix the assumption :)

Re: Line numbers in text box

Posted: Mon Mar 04, 2013 11:27 am
by bn
Hi Rod,

as Simon pointed out if you put the line number into the text field you will change the text and consequently how it breaks.
To avoid that I would use two fields as Simon proposed.

To calculate the line numbers of a wrapped text it is easiest to use the formattedText of a field. formattedText gives you the text as it is displayed but each displayed line has a return at the end.
Usually in LiveCode a line is considered all the text until a return is met. In other contexts this is also called a paragraph.

the code for a button to calculate line numbers:

Code: Select all

on mouseUp
   put the formattedText of field "fText" into tText -- or whatever the name of your text field is
   -- set the numberformat to "000" -- optional to display e.g. 001 instead of 1
   put 0 into tCounter
   put "" into tCollect
   repeat for each line aLine in tText
      add 1 to tCounter
      put tCounter & cr after tCollect
   end repeat
   delete last char of tCollect -- a return
   put tCollect into field "line number"
   set the vScroll of field "fText" to 0
end mouseUp
place the two fields of the same height next to each other, hide the vScrollbar in the property inspector for field "line number" and put Simons script into the field of your text

Code: Select all

on scrollbarDrag newValue
  set the vScroll of fld  "Line Number" to newValue
end scrollbarDrag
Kind regards

Bernd

Re: Line numbers in text box

Posted: Tue Mar 05, 2013 12:28 am
by Rod Edwardson
Thanks for your help everyone! I have it up and functioning perfectly. Slowly but surely getting the swing of things. Your assistance is truly appreciated.