Decimal Field

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
abanowBUSTfGf
Posts: 63
Joined: Sat Dec 22, 2012 3:01 am

Decimal Field

Post by abanowBUSTfGf » Sat Mar 23, 2013 4:11 am

How do I make a field a decimal field? I've created a text field and scripted it as below:
=======================================
on keyDown k
if k is a number then
pass keyDown
end if
end keyDown
==========================================

This forces only numbers 0 to 9 but
a) Does not allow decimal points and
b) When I press <Enter> it goes to the next line as it would in a text field
Tx

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Decimal Field

Post by sturgis » Sat Mar 23, 2013 4:28 am

This should work for limiting the field to numbers and a single "."

Code: Select all

on keydown k
   if k is a number then pass keydown
   if k is "." and k is not among the chars of me then pass keyDown
end keydown
As for a new line being entered when return is pressed there are a couple ways you can handle it. If you want to go to the next field when return is pressed you can turn on autotab (tab on return). If you want to stay focused on the current field when return or enter are pressed you can leave autotab turned off for the field and catch returninfield and enterinfield and do nothing with them. (meaning don't pass them)

so..

Code: Select all

on enterinfield

end enterinfield

on returninfield

end returninfield

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

Re: Decimal Field

Post by dunbarx » Sat Mar 23, 2013 4:30 am

Hi

For the decimal thing, you started with the "is a" operator. Good. Now think about the "is in" operator. Go to the dictionary. Can you see what this leads to? Write back if you need another hint.

As for the return thing, go to the dictionary and read up on the "returnInField" message. The use of this is slightly more subtle, to a new user. Write back on this as well.

I believe you can do all this on your own. The real flood of help will come from me and many others, but really only after you are more along in learning LC. Play around a lot.

Craig Newman

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am
Location: Warszawa / Poland

Re: Decimal Field

Post by snm » Sat Mar 23, 2013 7:39 pm

In addition to sturgis handler one line more to make possible negative numbers also:

Code: Select all

on keydown k
   if k is a number then pass keydown
   if k is "." and k is not among the chars of me then pass keyDown
   if k is "-" and word 2 of the selectedChunk is 1 then pass keydown
end keydown
Marek

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

Re: Decimal Field

Post by dunbarx » Sat Mar 23, 2013 9:47 pm

Well, since the cat is out of the bag, may as well do this compactly.

Code: Select all

on keyDown tKey
  if tKey is in "1234567890-." then pass keydown -- decimal and minus are in there, just hard to read
end keyDown

on returnInField -- return does not send a "keyDown" message. so must be handled separately.
end returnInField
Now then.

This has all sorts of real world, as opposed to ivory tower coding world, problems. So, abanowBUSTfGf, how do we make this actually work, as opposed to just having it look like it works? Please write back with the problems you see in all our previous posts. The solutions will take more effort, but will be fun.

EDIT: There are a couple of ways around the problems. Can you find several?

Craig Newman

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am
Location: Warszawa / Poland

Re: Decimal Field

Post by snm » Sat Mar 23, 2013 10:24 pm

I know my code can be not optimal, but

Code: Select all

on keyDown tKey
  if tKey is in "1234567890-." then pass keydown -- decimal and minus are in there, just hard to read
end keyDown
will allow you to type a lot of "-" and "." chars anywhere (e.g. "22-45.-.55" - it's not the number)

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

Re: Decimal Field

Post by dunbarx » Sat Mar 23, 2013 10:47 pm

Right.

Just limiting the chars that can be typed is only half the problem. We also have to deny a char that will make the current text become a non-number.

Get to it. Write back if you get stuck.

Craig Newman

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am
Location: Warszawa / Poland

Re: Decimal Field

Post by snm » Sat Mar 23, 2013 11:43 pm

I have problem with deny non latin characters (as "ą" where you keep "alt" key pressed typing "a"). Working on Mac Os X. I don't know why not always "optionKeyDown" message is sent to the field. If I have it done, I hope I have solution for decimal field with paste option working as well.

Marek

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

Re: Decimal Field

Post by dunbarx » Sun Mar 24, 2013 12:28 am

OK.

Try this in a field script. I only ask that you examine it until you see what is going on.

Code: Select all

on keyDown tKey
   put word 2 of the selectedChunk into tSelect
   switch
      case tSelect =1
         if  tKey &  me is a number then pass keyDown
         break
      case tSelect > 1
         if char 1 to tSelect of me & tKey & char (tSelect + 1) to the number of chars of me of me is a number then pass keyDown
         break
   end switch
end keyDown

on returnInField
end returnInField
This will validate the attempt to insert a character anywhere into a string, and if the attempt does not yield a valid number, the attempt is ignored. Right? It is likely that the user will try to edit the middle of the existing number, not just add chars to the end. And at any time a decimal or minus sign might be inserted.

NOW THEN: I left one trap in all this. Can you test until you find it? And when you do, can you see what you need to do to address this? Write back with questions or comments.

Craig Newman

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

Re: Decimal Field

Post by dunbarx » Tue Mar 26, 2013 2:56 am

How are we doing?

I couldn't resist, so put this in your field script:

Code: Select all

on keydown tKey
   put word 2 of the selectedChunk into tSelect
   get me
   put tKey before char tSelect of it
   if it is a number or it = "-"  or it = "." or it = "-." then pass keydown
end keydown
Craig Newman

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am
Location: Warszawa / Poland

Re: Decimal Field

Post by snm » Tue Mar 26, 2013 8:37 am

Craig,

I'm sorry for no answer, but I have very limited time because of my office urgent jobs. I'll come back to this soon.

Marek

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”