Add value of radio buttons to variable
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Add value of radio buttons to variable
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
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
-
- Posts: 253
- Joined: Wed Aug 19, 2015 4:29 pm
Re: Add value of radio buttons to variable
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:
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:
I think that should at least get you going.
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
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
Re: Add value of radio buttons to variable
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. The Button "Evaluate" got the following codes on.
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. 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
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. The Button "Evaluate" got the following codes on.
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. 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
-
- Posts: 253
- Joined: Wed Aug 19, 2015 4:29 pm
Re: Add value of radio buttons to variable
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.
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.
Re: Add value of radio buttons to variable
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 - Put it in the label instead, then use the reference to the label to get your total number to dictate your answer -
Re: Add value of radio buttons to variable
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.
Sry guys I don't have any experience in programming in general and I am using livecode for just two weeks . But thanks for all your help already!
Sry guys I don't have any experience in programming in general and I am using livecode for just two weeks . But thanks for all your help already!
-
- Posts: 253
- Joined: Wed Aug 19, 2015 4:29 pm
Re: Add value of radio buttons to variable
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.
Re: Add value of radio buttons to variable
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!
"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
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
...
So the script may try to add the label of a button that you did not mean, capisce?
Best
Klaus
Re: Add value of radio buttons to variable
I am using LiveCode Community 9.0.1. Thanks for helping that is really kind.
-
- Posts: 253
- Joined: Wed Aug 19, 2015 4:29 pm
Re: Add value of radio buttons to variable
Klaus,
I'm not sure I understand. The line as written is working as expected in my sample stack?
I'm not sure I understand. The line as written is working as expected in my sample stack?
Re: Add value of radio buttons to variable
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?
If it is working in your sample stack then you may have the group on the card in layer 1, right?
Re: Add value of radio buttons to variable
Hi Klaus
I think (hopefully ) I do get your point. I added your script to mine but the line is still throwing an error.
I think (hopefully ) 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
Re: Add value of radio buttons to variable
Same error as in the screenshot above?
-
- Posts: 253
- Joined: Wed Aug 19, 2015 4:29 pm
Re: Add value of radio buttons to variable
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.
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
Re: Add value of radio buttons to variable
It still throws the same error as before. I have sent my stack script to your email account ClipArtGuy.