Page 1 of 1
Validating a numeric entry in a datagrid column
Posted: Tue Aug 21, 2012 10:38 am
by korax_nyx
Hi:
I have a datagrid with two columns, the first column contains names, so normal text is entered there. The second column is "age" so only numbers go in that column.
When the user enters new data in the datagrid I can restrict that the field "age" is only composed by numbers, but when the user directly edits the datagrid with a double click in a filled cell, he can put letters in the age field. I want to restrict that so I found this in the forum
Code: Select all
on keyDown thePressedKey
if thePressedKey is in "0123456789" then
pass keyDown
else
beep
end if
end keyDown
But if I do that then it applies to every column of the datagrid and I want it to be applied only to the "age" column.
How can I use that script selectively in the desired column?
Thank you very much and sorry for my english, it's not my mother language.
Best regards
Re: Validating a numeric entry in a datagrid column
Posted: Tue Aug 21, 2012 2:41 pm
by dunbarx
Hi,
Put a rawkeydown handler in the datagrid (group) script:
Code: Select all
on rawKeyDown var
if (var > 47 and var < 58) or var = 65288 then pass rawKeyDown
end rawKeyDown
Note that you probably needed to handle "delete", hence the 65288.
To get the column of interest, try:
Code: Select all
on mouseup
put the dgColumn of the target into tColumn
end mouseup
Craig Newman
Re: Validating a numeric entry in a datagrid column
Posted: Wed Aug 22, 2012 8:30 am
by korax_nyx
Hi and thank you very much for your help.
The code regarding the rawKeyDown is very useful, thanks, but I can't make the two parts work together
When I put
Code: Select all
on mouseup
put the dgColumn of the target into tColumn
end mouseup
Everything is fine and the column is well identified, but when the rawKeyDown is executed, the tColumn variable does not preserve the value and it's empty, if I try this:
Code: Select all
on rawKeyDown var
put the dgColumn of the target into tColumn
if tColumn = "Age" then
if (var > 47 and var < 58) or var = 65288 then pass rawKeyDown
end if
end rawKeyDown
The dgColumn is not put into tColumn, so tColumn is always empty. I tried this in order to see if dgColumn works inside of the command rawKeyDown
Code: Select all
on rawKeyDown var
answer the dgColumn of the target
if (var > 47 and var < 58) or var = 65288 then pass rawKeyDown
end rawKeyDown
And the answer is always empty, so I can not track the column that way...
Any ideas?
Thank you very much, really.
Best regards
Re: Validating a numeric entry in a datagrid column
Posted: Wed Aug 22, 2012 2:07 pm
by dunbarx
Quite.
The goal here is to learn just how much fun LiveCode can be.
Your problem is to integrate two separate actions. First, to determine that the user is accessing a certain column, and second, that only certain characters be allowed to be entered within that column.
It can be determined that the user is in the allowed column, but this information has to be "learned" and remembered. We can do this a number of ways. My favorite is to use a custom property. I will call this property the "allowedColumn". We can set this property on the fly, and use it to validate that only the permitted column is being typed into. The property will be loaded on mouseUp, and that property will be checked each time the user presses a key.
Your job is to understand this, and more importantly, to experiment further. That is the price of the following two handlers, both of which are to be placed in the group script. Why do they work universally over the entire datagrid? How could I check the property status outside of the normal operation of the user? Step by step, how does it work? Why so many questions?
Code: Select all
on rawKeyDown var
if the allowedColumn of grp "yourGroup" = 3 and ((var > 47 and var < 58) or var = 65288) then pass rawKeyDown -- the "3" is up to you
end rawKeyDown
on mouseUp
set the allowedColumn of grp "yourGroup" to the dgColumnNumber of the target
end mouseup
Craig Newman
Re: Validating a numeric entry in a datagrid column
Posted: Wed Aug 22, 2012 3:38 pm
by Klaus
Hi all,
the best (and unfortunatly most complex) way to handle this is to apply a "custom behavior" to the "EditorField".
I still did not figure this out, but did not need to so far
Get the DataGrid PDF here
http://lessons.runrev.com/m/datagrid
and check chapter "Using The Built-In Field Editor".
Good luck
Best
Klaus
Re: Validating a numeric entry in a datagrid column
Posted: Wed Aug 22, 2012 7:54 pm
by bangkok
Here is an example with the "column behavior" method, but not with the "field editor" (when you edit the content of a cell) but instead an ask dialog, where the user types something.
Re: Validating a numeric entry in a datagrid column
Posted: Wed Aug 22, 2012 8:40 pm
by dunbarx
Klaus, Bankok.
One of the endearing things about LC is the number of ways to solve a problem. Behaviors are powerful tools, indeed.
My way is better.
Craig
Re: Validating a numeric entry in a datagrid column
Posted: Thu Aug 23, 2012 8:20 am
by korax_nyx
Thank you all, really. This forum is extremely useful and unselfish.
I'll try right away.
Best regards