keyInField

yves COPPE yvescoppe at skynet.be
Fri Sep 27 01:49:00 CDT 2002


Hi Jan

that's the final good code : BUT

local sBeforePaste

on keyDown pWhichKey

   put the selectedChunk into tChunk
   put word 2 of tChunk into tStart
   put word 4 of tChunk into tStop
   if tStart > tStop then-- no actual selection
     put (char 1 to tStop of me) & pWhichKey into tCheck
     if length(me) > theStart then
       put (char tStart to -1 of me) after tCheck
     end if
   else -- typing would replace the selected text
     put (char 1 to tStart - 1 of me) & pWhichKey into tCheck
     if length(me) > theStop then
       put (char tStop + 1 to -1 of me) after tCheck
     end if
   end if
   if Conv4Calc(tCheck) is a number then pass keyDown
   else beep
end keyDown

-- Reformat upon leaving the field

on closeField
   if "formatField" is not in the pendingMessages then send 
"formatField" to me in 5 milliseconds
end closeField

on formatField
   put the text of me into tNumber
   put Conv4Disp(tNumber) into me
end formatField

-- Handle pasting of text in this field
-- NOTE : doesn't work when Rev UI is ON

on pasteKey
   put the text of me into sBeforePaste
   if "checkAfterPaste" is not in the pendingMessages then send 
"checkAfterPaste" to me in 5 milliseconds
   pass pasteKey
end pasteKey

on checkAfterPaste
   put the text of me into tAfterPaste
   if Conv4Calc(tAfterPaste) is not a number then
     beep
     put sBeforePaste into me
   end if
end checkAfterPaste

-- Conversion between calculation and display formats

function Conv4Calc pNumber
   -- strip out the thousand separator (if any)
   put the uThousandSeparator of me into t1000Sep
   if t1000Sep is not empty then
     replace t1000Sep with "" in pNumber
   end if
   -- fix the decimal point for MetaCard/RunRev
   put the uDecimalPoint of me into tDecPoint
   if tDecPoint is not empty and tDecPoint <> "." then replace 
tDecPoint with "." in pNumber
   -- should now be a regular number
   return pNumber
end Conv4Calc

function Conv4Disp pNumber
   -- convert to the chosen floating point format
   put the uFormat of me into tFormat
   if tFormat is empty then put "%16.2f" into tFormat
   put format(tFormat, pNumber) into tNumber
   -- remove leading spaces
   put 1 into tStart
   repeat while char tStart of tNumber = " "
     add 1 to tStart
   end repeat
   -- and the next line was wrong of course
   -- put char tStart to -1 of pNumber into tNumber
   put char tStart to -1 of tNumber into tNumber
   -- prepare the display format
   -- thanks to Ken Ray for this RegExp :-)
   local tMinusHold,tMainNumber,tDecimalHold
   get 
matchText(tNumber,"([-]?)([0-9]*)[\.]?([0-9]*)",tMinusHold,tMainNumber, 
tDecimalHold)
   -- tweak tDecimalHold
   put the uDecimalPoint of me into tDecPoint
   if tDecPoint is empty then put "." into tDecPoint
   if tDecimalHold is not empty then put tDecPoint before tDecimalHold
   -- now determine how many separators to place
   put the uThousandSeparator of me into t1000Sep
   if t1000Sep is not empty then
     put length(tMainNumber) into tLength
     put (tLength DIV 3) into tSeps
     if (tLength MOD 3) = 0 then subtract 1 from tSeps
     -- insert the commas into the integer part
     repeat with i = tSeps down to 1
       put t1000Sep after char - (i * 3 + 1) of tMainNumber
     end repeat
   end if
   -- put everything back together
   return tMinusHold & tMainNumber & tDecimalHold
end Conv4Disp

--- End of script

Hope this suits everyone's needs ; all you need to do
is fill in the following custom properties
- uDecimalPoint (defaults to ".")
- uThousandSeparator (defaults to empty)
- uFormat (defaults to "%16.2f")


BUT

I do so :
uDecimalPoint : ","
uThousandSeparator : " "
uFormat : "%16.2f"

Now if I type :

1234567 -> 1 234 567,00
OK

BUT

1234567,2

I hope : 1 234 567,20

but I get an error !

can you develop your script a little further ?
-- 
Greetings.

Yves COPPE

Email : yvescoppe at skynet.be



More information about the use-livecode mailing list