Programming Gambas from Zip/RadioButtons

Radio Buttons and Groups Edit

Radio Buttons—one goes down, the others pop up

Radio buttons are like the buttons on the old cassette players. When you press down on one the others pop up. They are used for selecting one option among many.

If you have some radio buttons in a form, only one can be highlighted. Click one and the others clear. Even in code if you set one button’s highlight the others will unhighlight by themselves. The value property (boolean) indicates if the button is highlighted. When you click a radio button rb1 rb1.value = true happens automatically. When you click another button rb1.value = false happens automatically.

You might need two sets of radio buttons. To keep them separate, create them in a panel or some other container. Put the panel there first, and then make radio buttons in it, or select all the buttons you want to work together, right-click, and choose Embed in a container.

Another trick is to make them all share their events. Click any of the buttons and one and only one _Click event will fire. This avoids writing separate _Click handlers for each button. One handler does them all. But how do you know which button was clicked? Your one handler will probably want to do something based on which button was selected. This is where Last comes in. Last is the very last object that something happened to or that did something.


There are two sets of buttons, with each set in their own panel. rbRoad, rbSea and rbAir are in Panel1. rbApple, rbOrange and rbPear are in Panel2. The panels are the parents of their buttons. The buttons are their children.

The Group property for the road, sea and air buttons is set to rbTransport. It is as if they are acting like they are one single radio button, rbTransport.

The Group property for the apple, orange and pear buttons is set to rbFruit. It is as if they are one radio button, rbFruit.

Double-click one of the transport buttons (any one). You will find yourself writing a handler for the rbTransport group of buttons. Likewise, double-click one of the fruit buttons and you will find yourself writing a handler that rbApple, rbOrange and rbPear all respond to.

Public Sub rbTransport_Click()
  Message("You choose to travel by " & Last.text)

Public Sub rbFruit_Click()
  Message("I like " & LCase(Last.text) & "s too!")

The LCase function makes the text inside the brackets lower case. Run the program and click on buttons. In the next section, we add the ability to save settings to this program. Whatever state the buttons are in when we close the application will be the state to which they are restored when the application runs next time.

Programming Gambas from Zip
 ← Ascii RadioButtons SaveSettings →