Page 1 of 1

Quicken Date Behavior

Posted: Thu Jul 19, 2007 2:48 am
by asawyer13
Is there a way to globally make it so when the cursor is in a date field, and the user presses +, it will add one day to the current date, - sill subtract one day, and things like T or t will put today's date, M will put first day of the month, etc?

Alan

Posted: Thu Jul 19, 2007 6:56 am
by Janschenkel
Hi Alan,

Revolution doesn't really have the concept of 'date' fields - or 'decimal' fields or any other type of masked input. Every field is a rich text editor and any special behaviour must be scripted - with the exception that you can set a property to tab to the next control when the user hist the enter or return key.

Most people use a date picker to let the user enter a date - there are more out there, but the first link I found in my bookmarks was to:
http://www.troz.net/Rev/libraries.php

That being said, here's a bit of script that shows how you can intercept keypresses in a field and react accordingly - it doesn't do much in terms of input masking or formatting, though:

Code: Select all

on keyDown pKey
  -- NOTE: the keyup message is sent as the user presses the key
  -- first, if it's a digit, we'll allow the user to type it (the 'pass' command)
  if pKey is in "0123456789" then pass keyDown
  -- if it's a slash, we'll assume it's a separator and allow it
  if pKey is "/" then pass keyDown
  -- now handle the special keys
  switch pKey
    case "T"
    case "+"
    case "-"
    case "M"
      -- if it's a key that needs special care, handle it
      ModifyDate pKey
      break
    default
      -- if it's a key we don't allow, just beep and don't pass
      beep
      break
  end switch
end keyDown

on ModifyDate pKey
  -- first we get the date currently in the field
  -- and convert that into the dateItems format
  -- dateItems are formed 'yyyy,mm,dd,hh,mm,ss,wd'
  put the text of me into tDate
  convert tDate from system date to dateItems
  if the result is empty then
    put true into tIsValidDate
  else
    put false into tIsValidDate
  end if
  switch pKey
  case "T"  -- put today's date into the field
    put the system date into tDate
    break
  case "M"  -- go to the first day of the same month
    if tIsValidDate then
      -- we set the 'day' part of the date items to 1
      put 1 into item 3 of tDate
      convert tDate from dateItems to system date
    end if
    break
  case "+"  -- go to the next day
    if tIsValidDate then
      -- we increment the 'day' part and when this is
      -- converted back, Rev fixes the date if need be
      add 1 to item 3 of tDate
      convert tDate from dateItems to system date
    end if
    break
  case "-"  -- go to the previous day
    if tIsValidDate then
      -- we decrement the 'day' part and when this is
      -- converted back, Rev fixes the date if need be
      subtract 1 from item 3 of tDate
      convert tDate from dateItems to system date
    end if
    break
  end switch 
  -- finally update 
  put tDate into me
end ModifyDate
Hope this helped,

Jan Schenkel.

Posted: Thu Jul 19, 2007 11:02 am
by asawyer13
Thanks,
I will use your technique and see if I can make it work.
Thanks again,
Alan

Posted: Thu Jul 19, 2007 12:14 pm
by asawyer13
Jan,
Is there a way to create a function or something so that once I've created the script I want to use, then on each date field, I could just run the function so I wouldn't have to duplicate all the code for each field?
Alan

Posted: Thu Jul 19, 2007 1:03 pm
by Klaus
Hi Alan,

just put the "keydown" handler into any of your "date" fields and just change this line:

...
switch pKey
case "T"
case "+"
case "-"
case "M"
-- if it's a key that needs special care, handle it
put ModifyDate(pKey) into me
...

Then change and put the other handler into your stack script and change it like this:

FUNCTION ModifyDate pKey
...
...
end switch
## finally update
RETURN tDate
end ModifyDate


Best

Klaus

Posted: Thu Jul 19, 2007 11:46 pm
by xApple
That still makes you duplicate lots of code. Every one of your fields must have the switch pKey part. What if you want to change the key that calls ModifyDate() ? You would have to correct the "case M" line in *every* field.

A more elegant solution is to trap the KeyDown message in the card and then check the target that sent it. Either use the name or a custom propriety to check if the field applies to the ModifyDate() task.

Code: Select all

--in the card script
on keyDown pKey
  if "field" is not in the target then pass KeyDown
  if the dateActions of the target is not true then pass KeyDown
  switch pKey 
  case "M" 
    put ModifyDate(pKey,the target) into the target 
  end switch
end keyDown