Home

VCL Controls: The Radio Group

 
 

Introduction

There are two main ways you can create a group of radio buttons. We saw above that you can place them either on a group box or on a panel. Alternatively, the VCL provides a control specially made to create radio buttons: the RadioGroup control. To use it, first select it from the Standard tab of the Component Palette and position it on a form or other container.

A RadioGroup control is a special control used to create a group of radio buttons.

 

Characteristics of the RadioGroup Control

The RadioGroup control has its own mechanism of creating and managing its radio buttons. The radio buttons are created as strings. Therefore, after placing and drawing a RadioGroup control on a form or a frame, you can open its String List Editor from its Items property. You can then type a label for each radio button on its own line. Once you click OK, the radio buttons would be created and proportionately added to the RadioGroup. Although the strings appear as radio buttons, they only act like them with full functionality but they are not controls to their full extent. This means that these radio buttons do not have a name or any Windows control property, method, of events on their own.

Once the list of strings has been created, the container will take care of positioning the radio buttons so they can fit inside the host. If you type too many or too long strings for the host, resize the container.

The list of radio buttons of a RagioGroup control is a TStrings type represented on the TRadioGroup class as the property. Therefore, you can also programmatically create the list of radio buttons using the TStrings::Add() method as follows:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	grpEmplStatus->Items->Add("Part-Time");
	grpEmplStatus->Items->Add("Full-Time");
	grpEmplStatus->Items->Add("Contractor");
	grpEmplStatus->Items->Add("Consultant");
}
//---------------------------------------------------------------------------

Like the other radio buttons, one created using the RadioGroup control can also be selected. Since these radio buttons are stored in a string list, you can set which radio button is selected by changing the integer value of the ItemIndex property. Since no radio button is selected by default, its value is –1. The items in the string are counted starting at 0, then 1, and so on. For example, to set the second radio button as checked, set the ItemIndex property of the RadioGroup control to 1. This property can be changed only after the list is created. If you create the list programmatically, you can also decide which radio button would be selected when the list shows up. This is done by assigning a short integer value to the ItemIndex property. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	grpEmplStatus->Items->Add("Part-Time");
	grpEmplStatus->Items->Add("Full-Time");
	grpEmplStatus->Items->Add("Contractor");
	grpEmplStatus->Items->Add("Consultant");
	grpEmplStatus->ItemIndex = 2;
}
//---------------------------------------------------------------------------

This value should less than the total number of radio buttons. For example, if the RadioGroup control contains 4 strings, the ItemIndex value should be less than 4; in this case the value 0, 1, 2, or 3 would select a radio button, a –1 value would remove the dot from any radio button.

To distribute the radio buttons on different columns, you can use the Columns property on the Object Inspector. You can also do it at runtime:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	grpEmplStatus->Items->Add("Part-Time");
	grpEmplStatus->Items->Add("Full-Time");
	grpEmplStatus->Items->Add("Contractor");
	grpEmplStatus->Items->Add("Consultant");
	grpEmplStatus->ItemIndex = 2;
	grpEmplStatus->Columns = 2;
}
//---------------------------------------------------------------------------
 

RadioGroup Methods, Messages, and Events

The fastest and most convenient way to dynamically create a group of radio buttons consists of using a RadioGroup control. If you do not have this control already, either by adding it at design time or by creating anyhow, you can use the new operator to assign an instance of the TRadioGroup to the owner of this control. The compiler needs to know the owner that would have the responsibility of cleaning the RadioGroup once it is not needed anymore. This time, the owner should be the form, unless the RadioGroup control will be hosted by another container. Also, specify the parent of the control. If you will need the radio buttons in just one function or event, you can create the RadioGroup control in that function. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnCreateGroupClick(TObject *Sender)
{
	TRadioGroup* Group = new TRadioGroup(Form1);
	Group->Parent = Form1;
}
//---------------------------------------------------------------------------

If you are planning to use the control in more than one location, declare a TRadioGroup object in the private or public sections of the form or unit that would use it:

private: // User declarations
	TRadioGroup *grpMaritalStatus;
public: // User declarations
	__fastcall TForm1(TComponent* Owner);

To create the list that represents the radio buttons, use the TStrings::Items property of the RadioGroup control. You can do this in the function where you create the control locally:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnCreateGroupClick(TObject *Sender)
{
	TRadioGroup* Group = new TRadioGroup(Form1);
	Group->Parent = Form1;

	Group->Caption = "Membership";
	Group->Items->Add("Senior");
	Group->Items->Add("Adult");
	Group->Items->Add("Tean");
	Group->Items->Add("Child");
	Group->Columns = 2;
	Group->Left = 8;
	Group->Top = 20;
}
//---------------------------------------------------------------------------

If the RadioGroup was created globally, use the appropriate function or event to initialize it:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormDblClick(TObject *Sender)
{
	grpMaritalStatus = new TRadioGroup(Form1);
	grpMaritalStatus->Parent = Form1;

	grpMaritalStatus->Items->Add("Single");
	grpMaritalStatus->Items->Add("Married");
	grpMaritalStatus->Items->Add("Divorced");
	grpMaritalStatus->Items->Add("Widow");
	grpMaritalStatus->Left = 220;
	grpMaritalStatus->Top = 20;
	grpMaritalStatus->Height = 100;
	grpMaritalStatus->Width = 124;
	grpMaritalStatus->Caption = "Marital Status";
}
//---------------------------------------------------------------------------

Like most other visual controls, the RadioGroup fires the OnClick event when it is clicked. This event makes all radio buttons of the group to be considered as one. Therefore, when the user clicks this control, you can simply access the ItemIndex property to find out what button is checked.

Application:

Body Tag Formatter

Home Copyright © 2005-2012, FunctionX, Inc.