Setting a button to enabled only if fields complete

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
BranFlayk
Posts: 10
Joined: Sun Dec 01, 2013 4:10 pm

Setting a button to enabled only if fields complete

Post by BranFlayk » Sun Dec 01, 2013 4:21 pm

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

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Setting a button to enabled only if fields complete

Post by SparkOut » Sun Dec 01, 2013 4:52 pm

The code can sit in lots of places, there is no right or wrong answer here. One way is

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
In the script of each of the required fields you could

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
You 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 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
This 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.

BranFlayk
Posts: 10
Joined: Sun Dec 01, 2013 4:10 pm

Re: Setting a button to enabled only if fields complete

Post by BranFlayk » Sun Dec 01, 2013 5:51 pm

Thanks for the quick response. That makes a lot of sense, and I'll get that put into my code.

Post Reply