Hi all
I've been learning LiveCode this weekend by adapting a Python/Tkinter script I made at work for generating a report from a SQLite database.
Happy with how it has gone (lots of Google-ing to find things out), but now trying to fine tune it.
I've got a "start" button which checks input fields are filled in and then runs the report.
What I would like to try and do is
1) Stack loads, "Start" button is disabled
2) User fills in required fields
3) "Start" button gets enabled
I'm still a bit confused about the different places code can go (card, stack, controls) so not sure where this code would sit? There are 4 input fields that need to be filled for the script to run properly. Would there have to be a global variable somewhere that got set (but again, not sure where)?
Thanks in advance
Setting a button to enabled only if fields complete
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: Setting a button to enabled only if fields complete
The code can sit in lots of places, there is no right or wrong answer here. One way isIn the script of each of the required fields you couldYou could alternatively set the behavior of the fields to a behavior script for generic maintenance but that might be for another lesson.
In a script in the message path (probably the card script where the fields are) you couldThis is a quite simple way to deal with it. You could make things more elegant and/or generic with different structures and code (eg looping through the fields for each test, making a behavior script, etc) but hopefully this shows you a technique you can easily follow.
Code: Select all
on openStack
set the disabled of button "Start" [of card "Main" [of stack "ReportStack"]] to true
-- you can refer to the button directly and get as granular as you need to
end openStack
Code: Select all
on exitField
checkEntries
pass exitField --may not be required
end exitField
on closeField
checkEntries
pass closeField --may not be required
end closeField
In a script in the message path (probably the card script where the fields are) you could
Code: Select all
on checkEntries
put field "fieldA" into tField
if (tField is not an integer) or (tField < 0) or (tField > 100) then
-- fieldA must be a number in the range 0 to 100 to pass
exit checkEntries
end if
put field "fieldB" into tField
if (tField is empty) or (the length of tField < 4) or (the length of tField > 6) then
-- fieldB must be a string of length 4,5 or 6 to pass
exit checkEntries
end if
-- etc for the other fields
-- then if all the field checks have passed and execution has reached this point without
-- meeting an exit checkEntries statement, we can safely enable the start button
set the disabled of button "Start" to false
end checkEntries
Re: Setting a button to enabled only if fields complete
Thanks for the quick response. That makes a lot of sense, and I'll get that put into my code.