Page 1 of 1
Using check boxes in a group
Posted: Wed Feb 24, 2010 6:34 pm
by ukaussie
I am in the early stages of learning revTalk so sorry for this basic question...
I have a group with two check boxes (Yes and No). On selection I want to display some text in a field. There are four possible combinations (Yes only, No only, both Yes and No, and neither). At the moment i can get the script to work if only Yes is selected or only No is selected. What i can't work out is when both checkboxes are selected or when neither of them is selected (In these cases i want "I selected both" and "I selected none" to be shown in the field. This is what i have so far:
Code: Select all
on mouseup
if the hilite of button "Yes" is true
then put "I selected Yes!" into field "Field2"
if the hilite of button "No" is true
then put "I selected No!" into field "Field2"
end mouseup
I've tried all sorts of if/then statements also but none of them seemed to work. I'm sure they do, just my lack of knowledge!
Any suggestions would be much appreciated.
Grant.
Re: Using check boxes in a group
Posted: Wed Feb 24, 2010 8:48 pm
by Klaus
Hi Grant,
this is a situation where "if... then" will get to complex if this is doable with "if... then..." at all!
So this is a tyoical case for a "switch" structure:
Code: Select all
on mouseUp
put the hilite of btn "Yes" into tYes
put the hilite of btn "No" into tNo
switch
case tYes = true AND tNo = true
put "You selected both" into tAnswer
break
case tYes = false AND tNo = false
put "You selected none" into tAnswer
break
default
## Only two possibility left and we use "if... then" here:
if tYes = true then
put "You selected Yes" into tAnswer
else
put "you selected No" into tAnswer
end if
break
end switch
## All checking is done, now we can put the resulting string into the field:
put tAnswer into fld 1
end mouseUp
I usually use "switch" when it comes to more than two comparisons, since this is MUCH better readable!
Best
Klaus
Re: Using check boxes in a group
Posted: Wed Feb 24, 2010 11:41 pm
by BvG
Because I dislike switch (and to spite klaus) here's a if then else that works quite well:
Code: Select all
on mouseup
if the hilite of button "Yes" and the hilite of button "No" then --"is true" is assumed
put "I selected both!" into field "Field2"
else if the hilite of button "Yes" then
put "I selected Yes!" into field "Field2"
else if the hilite of button "No" then
put "I selected No!" into field "Field2"
else --nothing is selected
put "I selected None!" into field "Field2"
end if
end mouseup
Re: Using check boxes in a group
Posted: Thu Feb 25, 2010 9:24 am
by Klaus
Ts, ts, ts, Frechheit
But this is very hard to read or catch with one glance.
For readability I would script this with "if...then...“
Code: Select all
on mouseup
if the hilite of button "Yes" and the hilite of button "No" then
put "I selected both!" into field "Field2"
exit mouseup
end if
if the hilite of button "Yes" then
put "I selected Yes!" into field "Field2"
exit mouseup
end if
if the hilite of button "No" then
put "I selected No!" into field "Field2"
else
put "I selected None!" into field "Field2"
end if
end mouseup
Grant:
Try to NOT think in programming terms first!
Try to explain the problem in "plain english" to yourself.
Most of the time you can then translate this 1:1 into TevTalk.
You might want to check my "Memory" stack that I used for a lecture at a
Rev conference in Malta some years ago.
I tried to explain this way of thinking in this stack:
http://www.major-k.de/xtalke.html
"simple_memory1" down the page.
Best
Klaus
Re: Using check boxes in a group
Posted: Thu Feb 25, 2010 2:54 pm
by ukaussie
Hi Klaus and BvG,
Thanks for your excellent code examples! I tried both methods and both work. Thank you also for the code comments among the code...they really help to understand what the script is doing.
Thanks again!
Grant.