numeric field

Jan Schenkel janschenkel at yahoo.com
Thu Oct 10 09:54:03 EDT 2002


--- mark mitchell <cowhead at mac.com> wrote:
> 
> Jan wrote:
> 
> > Example: if you want a field that only accepts
> numeric
> > input, here's what you would do:
> >
> > 1) Java: create a new subclass JNumericField of
> > JTextField (i don't have my books at hand right
> now,
> > so it could be a different class) and make it so
> that
> > it only accepts numeric input. Then, whenever you
> need
> > a numeric field, you make an instance of that
> class.
> >
> > 2) RunRev: look at the use-revolution archive and
> copy
> > the script I posted a week or so ago ;-)
> >
> 
> I was too busy when you posted that, but I didn't
> understand why a very 
> simple script doesn't work for this.  I use this in
> a field script if I 
> want to restrict it to numeric input:
> 
> on keydown whichKey
>    if whichKey is a number then
>      pass keyDown
>    end if
> end keydown
> 
> It works fine.  What was the point of that long
> winded thing?
> 
> mark mitchell
> Japan
> 


Hi Mark,


The additional code covers several things:

1) if the user "pastes" data, you don't get a keydown
-- so you need a special construction to handle the
pasteKey (which, for additional fun, isn't sent when
the RunRev UI is running).

2) if you want to enable the user to type a decimal
number, you have to keep track of how many times he
typed a period -- so you have to keep an eye on
selections, and check if the new data after the
keyDown would still be a number.

3) if the data must be displayed in a format with
thousand separators or a comma instead of a point, you
need functions to convert back and forth between
display and calculation format.


On another note, I also checked my theory on using the
"insert script into front" construct; and the results
are very good.

- In the script mentioned above, replace 'me' with
'the target' or 'the text of the target' where needed.

- Put that script in a field "NumericInputOnly" in a
library stack named "myScriptLibrary".

- Change the code of any field you want to only accept
numeric input to:

  on openField
    insert script of field "NumericInputOnly" of \
      stack "myScriptLibrary" into front
  end openField
  on closeField
    remove script of field "NumericInputOnly" of \
      stack "myScriptLibrary" from front
  end closeField

- Incidentally, you could also have a backScript
somewhere that checks a fields 'uFieldFormat' custom
property and does the above transparently:

  on openField
    put the uFieldFormat of the target into \
      tFieldFormat
    switch tFieldFormat
    case "numeric"
      insert script of field "NumericInputOnly" of \
        stack "myScriptLibrary" into front
      break
    case "date"
      insert script of field "DateInputOnly" of \
        stack "myScriptLibrary" into front
      break
    -- insert other types of input here...
    default
      break
    end switch
    pass openField
  end openField
  on closeField
    put the uFieldFormat of the target into \
      tFieldFormat
    switch tFieldFormat
    case "numeric"
      remove script of field "NumericInputOnly" of \
        stack "myScriptLibrary" from front
      break
    case "date"
      remove script of field "DateInputOnly" of \
        stack "myScriptLibrary" from front
      break
    -- insert other types of input here...
    default
      break
    end switch
    pass closeField
  end closeField


As I promised before, I will wrap all this in a
template fields stack when I find the time.

Hope this cleared a few things up,

Jan Schenkel.


=====
"As we grow older, we grow both wiser and more foolish at the same time."  (La Rochefoucauld)

__________________________________________________
Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More
http://faith.yahoo.com



More information about the use-livecode mailing list