Home

VCL Controls: Radio Buttons

 

Introduction

A radio button is a control that appears as a round box. Normally, a radio button is accompanied by one or more other radio buttons that appear and behave as a group. The user decides what button is valid by clicking one of them. When a button has been clicked, its round box fills with a (big) dot. When one button is selected, the other round buttons of the (same) group are empty. The user can select another by clicking a new choice, which empties the previous selection. This technique of selecting is referred to as mutually-exclusive. 

To indicate what a particular radio button is used for, it is usually accompanied by a label. This static text informs the user as to what the control is used for.

Because they come in a group, radio buttons should be organized in a specific container. To implement their mutual exclusiveness, radio buttons should be positioned in either a RadioGroup, GroupBox, or a Panel controls. If you add the radio buttons on this type of container, by default, they will be treated as a group. You should refrain from positioning radio buttons directly on a form or a frame.

At design time, to create a group of radio buttons, first position a container, such as a panel or a group box, on the form. If you want each radio button to behave like a full control, first position a GroupBox or a panel controls on the form. To add each radio button, from the Standard tab of the Component Palette, click the RadioButton and click inside of the container.

Characteristics of Radio Buttons

From the programmerís standpoint, the most important property of any control is its Name. Therefore, after adding the radio buttons to a container, you can change their names using the Object Inspector.

Two properties are of particular importance to both you and the user: the label and the state of the control. The label is text that specifies what a particular radio button is used for. To set the label of a radio button, on the Object Inspector, click Caption and type the desired label. Repeat this for each radio button. If you want to change a radio buttonís caption at runtime, you can do so programmatically as follows:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	rdoGender1->Caption = "&Man";
}
//---------------------------------------------------------------------------

The second of the most important properties of a radio button is its state: whether it is selected or not. When the user clicks a radio button, it becomes exclusively selected. This is seen by the dot inside its rounded box ě. When a radio button is selected, it is said to be checked. By default, a newly created radio button is not checked. You can select a radio button using the Checked property. This same property allows you to decide what button would be selected when the form opens. As a Boolean property, to set the Checked state of a radio button, set this value to true. At runtime, to set a particular radio button as checked, assign a true value to its Checked property:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	rdoGender2->Checked = True;
}
//---------------------------------------------------------------------------

Once one button has been checked in a group, even if there was no selection in the beginning, one radio button is always selected in the group. The user cannot deselect all radio buttons. Only the developer can do this at runtime. By setting the Checked property of a radio button to false, you can remove its selection. Otherwise, you can assign a false value to the Checked property of each radio button.

When designing your radio buttons, to manage the space, you can distribute them on more than one column. If you want to use various columns on a group of radio buttons created using a GroupBox or a panel controls, you can visually position each radio button on the container. Programmatically, you can also change the Left and Top values of each control as desired.

By default, a radio button is configured so that the label would be positioned on the right side of the rounded box. This is controlled by the Alignment property. If you want the label on the left side, set the radio buttonís Alignment property accordingly. The possible values are: taRightJustify and taLeftJustify.

Radio Buttons Methods and Events

The primary method you will use with a radio button is the TRadioButton constructor. This member function is mostly used to dynamically create a radio button. Since all objects in C++ Builder must be called using pointers, to create a radio button, declare a pointer to TRadioButton and use the new operator to specify the control that owns the radio button. A bad idea would be to assign it to the form. As we have reviewed, you should never (or hardly) position a radio button or a group of radio buttons on a form. Therefore, before creating a radio button, you should have a container on your form. You can add such a host at design time or at run time.

If you want to programmatically create radio buttons, you must know that each button will be created as its own object, besides their container.

A radio button is just a special form of button. Its most used event is OnClick. After creating each radio button as a control, you can use its OnClick event to configure its functionality.

Application:

Operations

Home Copyright © 2005-2007 FunctionX, Inc.