Using Radio Buttons
This tutorial shows you how to create and use a group of radio buttons.
Introduction
Radio buttons usually occur in a cluster or group and mostly only allow you to select one of the buttons at a time. If you select a radio button in a group, all the other possible radio buttons are automatically unselected. This behavior is analogous to the way radio stations used to be selected by some analogue radios.
This tutorial shows you how to create a group of radio buttons and execute code with a particular button.
Creating a Group of Buttons
Start LiveCode and create a new stack by selecting New Stack from the File menu. Then start to create a group of radio buttons by dragging and dropping a number of radio button controls onto an area of the LiveCode stack (1). The position and alignment of the radio buttons is not important. Select each button in turn (2) and use the Property Inspector to update the properties of each button. The Property Inspector is launched by right clicking on the radio button you want to update and selecting Property Inspector. You may want to give each button a name in the Property Inspector (3). In this example, the radio buttons are called "Radio1", "Radio 2" and "Radio 3".
Where to add Code
There are a number of ways to add code to radio buttons and the place where you add the code is very much dependent on what you are trying to achieve. You can add code to each button, allowing each button to execute a particular set of instructions. You can add code to a group of radio buttons, allowing that group of buttons to execute a set of instructions that are specific to that group. You can add code to the card, that catches all button selections on a particular card, reacting to any group of buttons that you may have on a card.
This tutorial provides examples for adding code to button "Radio 1" and to the group in which the radio buttons are.
Adding Code to a Button
Right click on radio button "Radio 1" and select Edit Script. This opens the script editor, allowing you to write code that is specific to the button you selected. Add the following code:
on mouseUp
answer "You pressed Radio button 1" with "Okay"
end mouseUp
Click Apply to apply the code. This code waits for the mouseUp event for radio button "Radio 1" and opens a dialog box with the message "You pressed Radio button 1", when it is pressed.
This code is private to radio button "Radio 1" and is not executed by any other button.
Creating the Group
Before code can be added to the group of radio buttons, it is necessary to create a group with which the radio buttons are associated. This not only provides a logical unit for the radio buttons, it also indicates to LiveCode that the radio buttons belong together, allowing one radio button to be unselected when one other radio button is selected.
You create a group of radio buttons by selecting all of the radio buttons that are to appear in one group (1) by dragging over them with the mouse. Then select the Group button to create the group (2). The three selection boxes around the three radio buttons change to one large selection box that surrounds the radio button group.
Adding Code to the Group
Right click on the group you created in the previous step and select Edit Script. This opens the script editor, allowing you to write code that is specific to the group of button you created. Ensure you select the group, not the card or a radio button. To do this click on "Select Grouped" in the toolbar, then drag over the buttons.
If you have trouble selecting the group, you can also access it by opening the Project Browser (1) from the Tools menu, clicking on the expand icon (2) and clicking the tiny 0 opposite the group (3). The code editor for the group will open (4).
Add the following code:
on mouseUp
answer "You selected" && the short name of target with "Okay"
end mouseUp
This code waits for mouseUp events that are caused by controls in the group of radio buttons and opens a dialog box with the message: "You selected Radio 2" or "You selected Radio 3", depending on which button you selected. The message: "You selected Radio 1" is not raised because "Radio 1" has its own code that is executed when it is selected.
This code is private to the group of radio buttons.
Filip
I typed the literal code as in the example:
"on mouseUp
answer "You selected" && the short name of target with "Okay"
end mouseUp"
but nothing is happening. Do I need to replace the && and/or the 'short name' with another text? Do I have to duplicate the text?
I am completely new to livecode, and so far the tutorials have been great, but it is sometimes confusing when you guys are talking about something, but do not specify what exactly needs to be done.
Thanks for the help,
Elanor Buchanan
Hi Filip
This code looks okay, the only thing I can see that might be wrong is the quote marks. If you have put " at the start and end of the code it won't work. Other than that it should be okay.
I hope that helps.
Kind regards
Elanor
Gilson
I need to unselect all radio buttons on a group at run time.
Like other languages:
rbAnswer1.Value = false
rbAnswer2.Value = false
How can I do this in LiveCode?
Best Regards.
Elanor Buchanan
Hi Gilson
You can set the hilitedButton or hiltedButtonName property of the group to empty e.g.
set the hilitedButton of group "choice" to empty
Kind regards
Elanor
Gilson
Hi Elanor
Thank you so much for your answer.
I am developing a Quiz App for LiveCode learn purpose.
My intention is to use: radio buttons, check box, text entry field, combobox, labels, tab panel and basic table field.
I found this code at LiveCode forum:
on preOpenCard
set the hilitedbutton of grp "optRespostas" to 0
end preOpenCard
I think that empty and zero are the same thing.
Beautiful and/or right code?
Empty or zero?
Kind regards
Gilson
Elanor Buchanan
Hi Gilson
In this case you can use empty, zero or 0. These will all have the same effect and are all correct so in the case it's really just a style choice, whichever seems clearest to you or fits best with the rest of your code.
Kind regards
Elanor
Jim
I have a different goal. I have a series of eight groups of radio buttons (each of the eight groups includes five radio button choices). At the bottom I've included a button that, when clicked (on mouseup), should save the selected radio button data to a file. however, I'm having a hard time specifying the syntax for this. I've tried getting the currentValue of each radio button group, the hilite of each group, etc. Nothing works. I get a variety of errors (for example, "execution error at line 5 (Chunk: can't find background")
For such a "simple" programming task that I did in 10 minutes with Supercard, I'm having a devil of a time! Any suggestions would be appreciated.
Jim
Elanor Buchanan
Hi Jim
You have 3 options, you can get the hilitedButton, hilitedButtonID or hilitedButtonName of each group. The hilitedButton gives the number of the highlighted button in a group, the hilitedButtonID gives the ID of the highlighted button in a group and the hilitedButtonName gives the name of the highlighted button in a group.
I hope that helps.
Elanor
Andreas Laue
I tried to get these items by answering:
answer "You selected" && the short name of target && "/" && hilitedButton && "/" && hilitedButtonID && "/" && hilitedButtonName with "Okay" . The answer was (e.g.by Radio B"): "You selectedRadio B2 / hilitedButton / hilitedButtonID / hilitedButtonName" What's wrong here?
Elanor Buchanan
Hi Andreas
hilitedButton, hilitedButtonID and hilitedButtonName are properties of the group so you would need to refer to them as properties and specify the group in the code.
answer "You selected" && the short name of target && "/" && the hilitedButton of group 1 && "/" && the hilitedButtonID of group 1 && "/" && the hilitedButtonName of group 1 with "Okay" .
I hope that helps.
Elanor
Mike Moyle
I thought that I had only selected/grouped the buttons but, if I click anywhere OTHER than the buttons I get the response "You selected card ID 1002". Did I miss something?
Elanor Buchanan
Hi Mike
It sounds like you added the mouseUp handler to the card script rather than the group script. You can open the card script from the Object menu and see if the code is on there rather than the group script.
I hope that helps.
Kind regards
Elanor