Add value of radio buttons to variable

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

Tobi.hhu
Posts: 7
Joined: Mon Oct 22, 2018 6:17 pm

Add value of radio buttons to variable

Post by Tobi.hhu » Mon Oct 22, 2018 7:45 pm

Hi guys,
at the university, we have to submit self-programmed projects in a livecode seminar. I designed a simple questionnaire to add the values of the radio buttons together and comment on the neurological status depending on the value. So far I have not managed to add the values of the radio buttons to my variable "neuroscore". The radio buttons are arranged in groups so that only one number can be selected at a time. Can anyone tell me how to add the values of the radio buttons to the variable.

cheers,
Tobi
Screenshot 2018-10-22 at 20.36.14.png

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Add value of radio buttons to variable

Post by ClipArtGuy » Mon Oct 22, 2018 8:23 pm

This entry on radio buttons is worth a read

http://lessons.livecode.com/m/4603/l/56 ... io-buttons

It's going to be a bit tough without knowing more about what you have what you've tried, or named your groups and buttons, etc. If your radio buttons are named 1,2,3,4 ,5, you will definitely want to change that. Using integers as a name for an object, while not forbidden, can definitely lead to unexpected behavior.


One way this could work, is if you set the labels of the radio buttons to the number, you can then get the label like this:

Code: Select all

 put the label of btn (the hilitedbutton of grp "Your-Group-Of-Radio-Buttons-Here") into YourVariableHere
I'm not sure from just the screenshot, but it looks like you have three groups of radio buttons? If you only need to get those three answers and add them together, you could name your groups of radio buttons something like : radioGrp1,radioGrp2,etc you can then loop through the groups to get the total, like this:

Code: Select all

put 0 into tTotal
   repeat with x = 1 to 3
   add the label of btn (the hilitedbutton of grp ("radioGrp"&x) ) to tTotal
end repeat
answer tTotal
I think that should at least get you going.

Tobi.hhu
Posts: 7
Joined: Mon Oct 22, 2018 6:17 pm

Re: Add value of radio buttons to variable

Post by Tobi.hhu » Tue Oct 23, 2018 11:15 am

Hi thanks for your quick response,

Yes you are right. I've got three groups of radio button, consisting of 4 to 6 radio buttons.
But I think I need to get into more detail. So my stack consists of two cards. The one you have seen on the screenshot and the Feedback-card.
Screenshot 2018-10-23 at 12.02.30.png
The Button "Evaluate" got the following codes on.
Screenshot 2018-10-23 at 12.02.42.png

The values of the radio buttons should add up, so that on the feedback card pop up different answers depending on the value.
I put this code on the Feedback-card.
Screenshot 2018-10-23 at 12.03.52.png
I renamend the "radio groups" to "radioGrp1....radioGrp3" though I am still not sure if its possibe to add the different label still some of them are namend the same. I could do it on different card, using 1 card per question but as it is a simple questionaire it would be more handy if it is on one card. I already used different codes to the groups such as "if hilitedbutton = 4 add 4 to neurscore" but it is not working. In case of your idea do I have to put the code into the button script?

Thanks for your help
Tobi

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Add value of radio buttons to variable

Post by ClipArtGuy » Tue Oct 23, 2018 2:01 pm

Here's a small sample stack with three groups of radio buttons on card 1, the labels of the buttons are set to the number values, again the button names should NOT be numbers.

The groups are named as mentioned previously, radioGrp1, etc. The script to add the totals is in the "tally" button. The total is put into the global variable "neuroscore", and then moves to the next card.

The script to display the result is in the card script of card 2 script in a preopencard handler.

I think this should get you pointed in the right direction, but I'm sure someone else will chime in if there's a better way.
RadioSample.zip
(1.14 KiB) Downloaded 193 times

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Add value of radio buttons to variable

Post by bogs » Tue Oct 23, 2018 2:30 pm

ClipArtGuy wrote:
Tue Oct 23, 2018 2:01 pm
the button names should NOT be numbers.
Maybe Tobi is being confused by names vs. labels? In case that is the problem, this is what ClipArtGuy is saying.

Instead of putting the number in the name of the radio button, like this first picture -
Selection_080.png
1 in name...
Put it in the label instead, then use the reference to the label to get your total number to dictate your answer -
Selection_081.png
1 in label...
Selection_081.png (15.03 KiB) Viewed 7551 times
Image

Tobi.hhu
Posts: 7
Joined: Mon Oct 22, 2018 6:17 pm

Re: Add value of radio buttons to variable

Post by Tobi.hhu » Tue Oct 23, 2018 3:09 pm

Thanks ClipArtGuys for your script! I did as you guys said. I double checked but my radio buttons do have different names. So labels and names are different. I even used the same names as in ClipArtGuys script and put his script into mine. Though it still doesn't work.
Screenshot 2018-10-23 at 16.02.01.png
Screenshot 2018-10-23 at 16.02.09.png
Sry guys I don't have any experience in programming in general and I am using livecode for just two weeks :roll: :lol: . But thanks for all your help already!

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Add value of radio buttons to variable

Post by ClipArtGuy » Tue Oct 23, 2018 3:35 pm

Hmm...That line shouldn't be throwing an error if the groups and buttons are named and labeled correctly. I guess it's worth asking what version of LC you are using? I know you you can't upload files to the forums yet, but if you wanted to send me a copy of your stack I will take a look.
Last edited by ClipArtGuy on Wed Oct 24, 2018 12:56 am, edited 1 time in total.

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Add value of radio buttons to variable

Post by Klaus » Tue Oct 23, 2018 3:48 pm

Hi Tobi,

1. welcome to the forum!

2. Could you please post your code as TEXT and not as image? So we can copy/paste.
Paste the script(s) here, then select the script and select the CODE tags (button 5 from the left above this field). That will also preserve the formatting!

3. You are missing the exact definition of your buttons inside of the repeat loop!

Code: Select all

...
repeat with x = 1 to 3
  add the label of btn (the hilitebutton of grp ("radiogrp" & x)) OF GRP ("radiogrp" & x) to neuroscore
end repeat
...
"the hilitedbutton of grp xyz" returns the NUMBER of that button, but relative to the group, so it may be "button 1 of grp xyz" but actually button number 47 on the card.

So the script may try to add the label of a button that you did not mean, capisce?


Best

Klaus

Tobi.hhu
Posts: 7
Joined: Mon Oct 22, 2018 6:17 pm

Re: Add value of radio buttons to variable

Post by Tobi.hhu » Tue Oct 23, 2018 3:51 pm

I am using LiveCode Community 9.0.1. Thanks for helping that is really kind.

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Add value of radio buttons to variable

Post by ClipArtGuy » Tue Oct 23, 2018 3:54 pm

Klaus,

I'm not sure I understand. The line as written is working as expected in my sample stack?

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Add value of radio buttons to variable

Post by Klaus » Tue Oct 23, 2018 4:00 pm

I am referring to Tobi's screenshot of the script editor for btn "Evaluate".
If it is working in your sample stack then you may have the group on the card in layer 1, right?

Tobi.hhu
Posts: 7
Joined: Mon Oct 22, 2018 6:17 pm

Re: Add value of radio buttons to variable

Post by Tobi.hhu » Tue Oct 23, 2018 4:16 pm

Hi Klaus
I think (hopefully :roll: ) I do get your point. I added your script to mine but the line is still throwing an error.

Code: Select all

global VPname, neuroscore

on mouseup
   put 0 into neuroscore
   repeat with x = 1 to 3
      add the label of btn (the hilitedbutton of grp ("radiogrp" & x)) of grp ("radiogrp" & x) to neuroscore
   end repeat
   put empty into VPname
   put fld "name" of card "GCS" into line 1 of VPname
   put label of button "Sex" of card "GCS" into line 2 of VPname
   if fld "name" is not empty and label of button "Sex" is not "choose sex" then
      go next card 
   else
      answer "Fill in all fields"
      
   end if
end mouseup

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Add value of radio buttons to variable

Post by Klaus » Tue Oct 23, 2018 4:17 pm

Same error as in the screenshot above?

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Add value of radio buttons to variable

Post by ClipArtGuy » Tue Oct 23, 2018 4:20 pm

Thanks for the clarification Klaus. I get it now.

Tobi,

I just changed the button script to this in your stack, as per Klaus' advice, and it seems to be working as expected now.

Code: Select all

global VPname, neuroscore

on mouseup
   put 0 into neuroscore
   repeat with x = 1 to 3
      add the label of btn (the hilitedbutton of grp ("radiogrp" & x)) of grp ("radiogrp" & x) to neuroscore
   end repeat
   put empty into VPname
   put fld "name" of card "GCS" into line 1 of VPname
   put label of button "Sex" of card "GCS" into line 2 of VPname
   if fld "name" is not empty and label of button "Sex" is not "choose sex" then
      go next card 
 
   else
      answer "Fill in all fields"
      
   end if
end mouseup

Tobi.hhu
Posts: 7
Joined: Mon Oct 22, 2018 6:17 pm

Re: Add value of radio buttons to variable

Post by Tobi.hhu » Tue Oct 23, 2018 4:29 pm

It still throws the same error as before. I have sent my stack script to your email account ClipArtGuy.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”