Decimal Field
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 63
- Joined: Sat Dec 22, 2012 3:01 am
Decimal Field
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
=======================================
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
Re: Decimal Field
This should work for limiting the field to numbers and a single "."
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 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
so..
Code: Select all
on enterinfield
end enterinfield
on returninfield
end returninfield
-
- VIP Livecode Opensource Backer
- Posts: 9785
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Decimal Field
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
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
-
- VIP Livecode Opensource Backer
- Posts: 253
- Joined: Fri Dec 09, 2011 11:17 am
- Location: Warszawa / Poland
Re: Decimal Field
In addition to sturgis handler one line more to make possible negative numbers also:
Marek
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
-
- VIP Livecode Opensource Backer
- Posts: 9785
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Decimal Field
Well, since the cat is out of the bag, may as well do this compactly.
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
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
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
-
- VIP Livecode Opensource Backer
- Posts: 253
- Joined: Fri Dec 09, 2011 11:17 am
- Location: Warszawa / Poland
Re: Decimal Field
I know my code can be not optimal, but
will allow you to type a lot of "-" and "." chars anywhere (e.g. "22-45.-.55" - it's not the number)
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
-
- VIP Livecode Opensource Backer
- Posts: 9785
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Decimal Field
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
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
-
- VIP Livecode Opensource Backer
- Posts: 253
- Joined: Fri Dec 09, 2011 11:17 am
- Location: Warszawa / Poland
Re: Decimal Field
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
Marek
-
- VIP Livecode Opensource Backer
- Posts: 9785
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Decimal Field
OK.
Try this in a field script. I only ask that you examine it until you see what is going on.
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
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9785
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Decimal Field
How are we doing?
I couldn't resist, so put this in your field script:
Craig Newman
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
-
- VIP Livecode Opensource Backer
- Posts: 253
- Joined: Fri Dec 09, 2011 11:17 am
- Location: Warszawa / Poland
Re: Decimal Field
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
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