Radio Buttons and Check Boxes

Radio buttons and check boxes are specialized buttons but are, in the end, still buttons. I'm not going to spend a lot of time discussing these two buttons because implementing them is pretty straightforward. Both the RadioButton and CheckBox components have a property called Checked that can be used to set the check state and can be read to retrieve the current check state.

The radio button is usually used in a group of buttons. A radio button typically signifies a group of options, only one of which can be selected at one time (like a group of speed buttons, which you just learned about). Although you can use a radio button by itself, it is not recommended because it is confusing to your users. When tempted to use a radio button by itself, use a check box instead—that's what a check box is for, after all.

Any buttons placed on a form will automatically be considered part of the same group. If you have more than one group of radio buttons, and those groups need to operate independently of one another, you need to use a RadioGroup component. This component allows you to quickly set up a group of radio buttons with a 3D frame around the buttons and a caption as well. To illustrate this concept, do the following exercise:

  1. Create a blank form or use the form you created in the previous exercise. Place a RadioGroup component on the form.
  2. Locate the Items property and double-click the Value column.
  3. The String list editor is displayed. Type the following lines in the String list editor:

Redtailed Hawk Peregrine Falcon Gyrfalcon Northern Goshawk

  1. Click OK to close the String list editor. The group box is populated with radio buttons containing the text you typed.
  2. Change the Caption property of the radio group box to Apprentice Falconers Can Legally Possess:.
  3. Click Run to compile and run the program.

When you click one of the radio buttons, the previously selected button pops up as expected. Using the RadioGroup component, you can put more than one group of radio buttons on a form. Like the ListBox and ComboBox components discussed earlier, the RadioGroup component has an ItemIndex property that you can read at runtime to determine which item in the group is selected. Oh, by the way—the answer to the quiz is Redtailed Hawk (American Kestrel would also have been an acceptable answer, but it was not presented in the list).

Note

You can also use a GroupBox component to hold radio buttons. The GroupBox component is less convenient to use than the RadioGroup component, but it has more flexibility. You can place any type of control in a group box. Once placed in the group box, the controls and the group box itself can be moved as a unit at design time.

The CheckBox component is used to allow users to turn an option on or off or to indicate to a user that an option is currently on or off. A check box can have up to three states, depending on its style: on, off, or grayed. If the check box's AllowGrayed property is false, it can only be checked or unchecked. When the AllowGrayed property is true, the check box can be any one of the three states. The grayed, or indeterminate, state is handled programmatically. In other words, it's up to you to decide what the grayed state means for your application. If the AllowGrayed property is false (the default), you can use the Checked property to determine whether the check box is checked or unchecked. If the AllowGrayed property is true, you must use the State property to determine (or set) the check box state. State will return either cbChecked, cbUnchecked, or cbGrayed.

To make a check box read-only but not grayed, place it on a panel and change the panel's Enabled property to false.

+1 0

Responses

  • Halle
    When can the checked property of a radio button be set to either true or false delphi?
    8 years ago
  • Dodinas
    How to hold selected radio button in groupbox?
    8 years ago
  • kathryn
    Can you select more than one radiobutton in delphi?
    6 years ago

Post a comment