Using check boxes in a group

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ukaussie
Posts: 23
Joined: Mon Sep 28, 2009 9:04 pm

Using check boxes in a group

Post by ukaussie » Wed Feb 24, 2010 6:34 pm

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.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using check boxes in a group

Post by Klaus » Wed Feb 24, 2010 8:48 pm

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

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Using check boxes in a group

Post by BvG » Wed Feb 24, 2010 11:41 pm

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
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using check boxes in a group

Post by Klaus » Thu Feb 25, 2010 9:24 am

Ts, ts, ts, Frechheit :D

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

ukaussie
Posts: 23
Joined: Mon Sep 28, 2009 9:04 pm

Re: Using check boxes in a group

Post by ukaussie » Thu Feb 25, 2010 2:54 pm

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.

Post Reply