Home

Win32 Controls: The Radio Group Control

 

Introduction to the Radio Group Control

 

Description

A radio group is a control container specially made to hold a group of radio buttons. The radio buttons in its body are created not as radio buttons but as strings, or the radio buttons are considered as a collection of items.

 

Practical LearningPractical Learning: Introducing the Radio Group

  1. To create a new project, on the main menu, click File -> New -> VCL Forms Application - C++Builder
  2. In the Object Inspector, change the following properties:
    Caption: Oblique Triangles
    Name:    frmCalculations
    Position: poScreenCenter

 

Creating a Radio Group Control

To create radio buttons, you can add a group box or a panel, and then add TRadioButton controls to the container. To give you a convenient way to create radio buttons, the VCL provides the radio group. It is available through a class named TRadioGroup. In reality, the TRadioGroup class implements TCustomRadioGroup. The TCustomRadioGroup class is derived from TCustomGroupBox:

TRadioGroup Inheritance

To visually create a radio group, in the Standard section of the Tool Palette, click the TRadioGroup control TRadioGroup and click the form or other container.

The fastest and most convenient way to dynamically create a group of radio buttons consists of using a TRadioGroup 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 radio group once it is not needed anymore. This time, the owner should be the form, unless the radio group 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 radio group 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);

Practical LearningPractical Learning: Creating a Radio Group

  1. In the Tool Palette, click Standard
  2. Click the TRadioGroup icon TRadioGroup and click the form

Characteristics of a Radio Group

 

Introduction

A radio group is primarily a Windows control. As such, it has a name, a location, and a size. You can specify the values of these properties when initializing the control. Because a radio group is derived from TControl, it displays text through its Caption property.

Here is example of initializing a radio group:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnCreateGroupClick(TObject *Sender)
{
	TRadioGroup* Group = new TRadioGroup(Form1);
	Group->Parent = Form1;
	
	Group->Caption = "Membership";
	Group->Left = 8;
	Group->Top = 20;
}
//---------------------------------------------------------------------------

Practical LearningPractical Learning: Introducint the Characteristics of a Radio Group

  • On the form, make sure the radio group is still selected.
    In the Object Inspector, change the following properties:
    Caption: Select the Categories of Known Values
    Name:    grpCategories

The Items of a Radio Group

To identify, hold, and manage its radio buttons, a radio group has a collection property named Items that is of type TStrings:

__property Classes::TStrings * Items = {read=FItems,write=SetItems};

To visually create the radio buttons, access the Object Inspector for the radio group, click Items, and click its ellipsis button. This would open the String List Editor. In it, type a string for each radio button, each string on its own line, and click OK.

To programmatically create the radio buttons, access the Items property of the TRadioGroup variable, call the Add() method on it, and pass a string as argument. Here are examples:

//---------------------------------------------------------------------------
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->Left = 8;
	Group->Top = 20;
}
//---------------------------------------------------------------------------

If the radio group 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->Caption = "Marital Status";
	grpMaritalStatus->Left = 220;
	grpMaritalStatus->Top = 20;
	grpMaritalStatus->Height = 100;
	grpMaritalStatus->Width = 124;

	grpMaritalStatus->Items->Add("Single");
	grpMaritalStatus->Items->Add("Married");
	grpMaritalStatus->Items->Add("Divorced");
	grpMaritalStatus->Items->Add("Widow");
}
//---------------------------------------------------------------------------

Practical LearningPractical Learning: Creating the Items

  1. On the form, make sure the radio group is still selected.
    In the Object Inspector, click Items and click its button
  2. In the String List Editor, type AAS - Known Values: Two angles and a side opposite one of them and press Enter
  3. Type ASA - Known Values: Two angles and the side between them and press Enter
  4. Type SAS - Known Values: Two sides and the angle between them and press Enter
  5. Type SSS - Known Values: All three sides and press Enter
     
    String List Editor
  6. Click OK

Wrapping a Radio Button

Although most radio buttons use a short string, sometimes you want to use a long string. If you do, its text may span beyond the width of the radio group and part of the end of the string may hide. As an alternative, you can make the text of the radio buttons use more than one line if necessary. To support this, the TCustomRadioGroup class has a Boolean property named WordWrap:

__property bool WordWrap = {read=FWordWrap,write=SetWordWrap};

IIf you set this property to True, if the caption of a radio button is short enough to fit the width of the radio group, it would show on one line. If the caption is longer, it would use more than one line to display its string.

Practical LearningPractical Learning: Creating a Radio Group

  1. On the form, make sure the radio group is still selected.
    In the Object Inspector, click the check box of WordWrap to make it True
  2. Complete the design of the form as follows:

    Oblique Triangles
    Control Name Caption/Text Other Properties
    TRadioGroup TRadioGroup      
    TPanel TPanel pnlAAS    
    TImage Edit     Picture: triangle1.bmp
    TStaticText Directory List Box   Known Values  
    TStaticText Directory List Box   Unknown Values  
    TLabel Directory List Box   Angle 1:  
    TEdit Edit edtAASAngle1 0.00 Alignment: taRightJustify
    TButton TBitBtn btnAASCalculate Calculate  
    TLabel Directory List Box   Angle 3:  
    TEdit Edit edtAASAngle3 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Angle 2:  
    TEdit Edit edtAASAngle2 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Side 2:  
    TEdit Edit edtAASSide2 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Side 1:  
    TEdit Edit edtAASSide1 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Side 3:  
    TEdit Edit edtAASSide3 0.00 Alignment: taRightJustify
    TButton TBitBtn btnClose Close  
     
  3. Right-click the panel (an unoccupied area on it) -> Edit -> Copy
  4. Right-click an occupied area of the form -> Edit -> Paste
  5. While the new panel is still selected, in the Object Inspector, change its Name to pnlASA
  6. Position the newly pasted panel on top of the first one
  7. Change its design as follows:
     
    Oblique Triangles
    Control Name Caption/Text Other Properties
    TRadioGroup TRadioGroup      
    TPanel TPanel pnlASA    
    TImage Edit     Picture: triangle2.bmp
    TStaticText Directory List Box      
    TStaticText Directory List Box      
    TLabel Directory List Box   Angle 1:  
    TEdit Edit edtASAAngle1 0.00 Alignment: taRightJustify
    TButton TBitBtn btnASACalculate Calculate  
    TLabel Directory List Box   Angle 3:  
    TEdit Edit edtASAAngle3 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Angle 2:  
    TEdit Edit edtASAAngle2 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Side 2:  
    TEdit Edit edtASASide2 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Side 1:  
    TEdit Edit edtASASide1 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Side 3:  
    TEdit Edit edtASASide3 0.00 Alignment: taRightJustify
     
  8. Right-click the panel (an unoccupied area on it) -> Edit -> Copy
  9. Right-click an occupied area of the form -> Edit -> Paste
  10. While the new panel is still selected, in the Object Inspector, change its Name to pnlSAS
  11. Position the newly pasted panel on top of the other
  12. Change its design as follows:
    Oblique Triangles
    Control Name Caption/Text Other Properties
    TRadioGroup TRadioGroup      
    TPanel TPanel pnlSAS    
    TImage Edit     Picture: triangle2.bmp
    TStaticText Directory List Box      
    TStaticText Directory List Box      
    TLabel Directory List Box   Angle 1:  
    TEdit Edit edtSASAngle1 0.00 Alignment: taRightJustify
    TButton TBitBtn btnSASCalculate Calculate  
    TLabel Directory List Box   Angle 3:  
    TEdit Edit edtSASAngle3 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Angle 2:  
    TEdit Edit edtSASAngle2 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Side 2:  
    TEdit Edit edtSASSide2 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Side 1:  
    TEdit Edit edtSASSide1 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Side 3:  
    TEdit Edit edtSASSide3 0.00 Alignment: taRightJustify
     
  13. Right-click the panel (an unoccupied area on it) -> Edit -> Copy
  14. Right-click an occupied area of the form -> Edit -> Paste
  15. While the new panel is still selected, in the Object Inspector, change its Name to pnlSSS
  16. Position the newly pasted panel on top of the other
  17. Change its design as follows:
    Oblique Triangles
    Control Name Caption/Text Other Properties
    TRadioGroup TRadioGroup      
    TPanel TPanel pnlSSS    
    TImage Edit     Picture: triangle2.bmp
    TStaticText Directory List Box      
    TStaticText Directory List Box      
    TLabel Directory List Box   Side 1:  
    TEdit Edit edtSSSSide1 0.00 Alignment: taRightJustify
    TButton TBitBtn btnSSSCalculate Calculate  
    TLabel Directory List Box   Side 2:  
    TEdit Edit edtSSSSide2 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Side 3:  
    TEdit Edit edtSSSSide2 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Angle 1:  
    TEdit Edit edtSSSAngle1 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Angle 2:  
    TEdit Edit edtSSSAngle2 0.00 Alignment: taRightJustify
    TLabel Directory List Box   Angle 3:  
    TEdit Edit edtSSSAngle3 0.00 Alignment: taRightJustify
     

The Buttons of a Radio Group

Although the items of a radio group are created as strings, each is actually a whole radio button that is in fact a TRadioButton object. To identify the items of the group as objects, the TCustomRadioGroup class is equipped with a property named Buttons:

__property Stdctrls::TRadioButton * Buttons = {read=GetButtons};

Notice that this is a read-only property. This means that you cannot use the TCustomRadioGroup::Buttons property to create a radio button. Instead, you can access the TRadioButton part of a radio group item and do something with it.

Accessing a Radio Button

As seen already, the radio buttons of a radio group are stored in a collection of strings. Each can be accessed using an index. To let you access a particular radio button, the TCustomRadioGroup class provides the integer ItemIndex property:

__property int ItemIndex = {read=FItemIndex,write=SetItemIndex};

Since no radio button is selected by default, the primary value of the TCustomRadioGroup::ItemIndex property 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 radio group 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 be less than the total number of radio buttons. For example, if the radio group 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.

Practical LearningPractical Learning: Selecting a Radio Button

  1. Double-click an occupied area of the form to generate its OnCreate event
  2. Implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmCalculations::FormCreate(TObject *Sender)
    {
    	grpCategories->ItemIndex = 0;
    
    	pnlAAS->Visible = True;
    	pnlASA->Visible = False;
    	pnlSAS->Visible = False;
    	pnlSSS->Visible = False;
    }
    //---------------------------------------------------------------------------

The Columns of Radio Buttons

To distribute the radio buttons on different columns, you can use the Columns property:

__property int Columns = {read=FColumns,write=SetColumns};

You can visually set its value in the Object Inspector or programmatically. 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;
	grpEmplStatus->Columns = 2;
}
//---------------------------------------------------------------------------

Using a Radio Button in the Group

Like most other visual controls, the radio group 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.

Practical LearningPractical Learning: Using a Radio Group

  1. On the form, double-click the radio group
  2. Implement its OnClick event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmCalculations::grpCategoriesClick(TObject *Sender)
    {
        if(	grpCategories->ItemIndex == 0 )
        {
    	pnlAAS->Visible = True;
    	pnlASA->Visible = False;
    	pnlSAS->Visible = False;
    	pnlSSS->Visible = False;
        }
        else if( grpCategories->ItemIndex == 1 )
        {
    	pnlASA->Visible = True;
    	pnlAAS->Visible = False;
    	pnlSAS->Visible = False;
    	pnlSSS->Visible = False;
        }
        else if( grpCategories->ItemIndex == 2 )
        {
    	pnlSAS->Visible = True;
    	pnlASA->Visible = False;
    	pnlAAS->Visible = False;
    	pnlSSS->Visible = False;
        }
        else // if( grpCategories->ItemIndex == 3 )
        {
    	pnlSSS->Visible = True;
    	pnlSAS->Visible = False;
    	pnlASA->Visible = False;
    	pnlAAS->Visible = False;
        }
    }
    //---------------------------------------------------------------------------
  3. In the combo box on top of the Object Inspector, select btnAASCalculate and click Events
  4. Double-click OnClick and implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmCalculations::btnAASCalculateClick(TObject *Sender)
    {
    	double Angle1 = 0.00, Angle2 = 0.00, Angle3 = 0.00;
    	double Side1  = 0.00, Side2  = 0.00, Side3  = 0.00;
    
    	Angle1 = StrToFloat(edtAASAngle1->Text);
    	Angle2 = StrToFloat(edtAASAngle2->Text);
    	Side1  = StrToFloat(edtAASSide1->Text);
    
    	// Here, we use the law of sines
    	Angle3 = 180 - (Angle1 + Angle2);
    	Side2  = Side1 * sin(Angle2 * M_PI / 180) / sin(Angle1 * M_PI / 180);
    	Side3  = Side1 * sin(Angle3 * M_PI / 180) / sin(Angle1 * M_PI / 180);
    
    	edtAASAngle3->Text = FloatToStrF(Angle3, ffFixed, 6, 2);
    	edtAASSide2->Text  = FloatToStrF(Side2, ffFixed, 6, 6);
    	edtAASSide3->Text  = FloatToStrF(Side3, ffFixed, 6, 6);
    }
    //---------------------------------------------------------------------------
  5. In the combo box on top of the Object Inspector, select btnASACalculate and click Events
  6. Double-click OnClick and implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmCalculations::btnASACalculateClick(TObject *Sender)
    {
    	btnAASCalculateClick(Sender);
    }
    //---------------------------------------------------------------------------
  7. In the combo box on top of the Object Inspector, select btnSASCalculate and click Events
  8. Double-click OnClick and implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmCalculations::btnSASCalculateClick(TObject *Sender)
    {
    	double Angle1 = 0.00, Angle2 = 0.00, Angle3 = 0.00;
    	double Side1  = 0.00, Side2  = 0.00, Side3  = 0.00;
    
    	Side1  = StrToFloat(edtSASSide1->Text);
    	Side2  = StrToFloat(edtSASSide2->Text);
    	Angle1 = StrToFloat(edtSASAngle1->Text);
    
    	// Here, we use the law of cosines
    	Side3  = sqrt((Side1 * Side1) +
    				  (Side2 * Side2) -
    				  (2 * Side1 * Side2 * cos(Angle1 * M_PI / 180)));
    	Angle2 = acos(((Side3 * Side3) +
    				  (Side2 * Side2) -
    				  (Side1 * Side1)) /
    				 (2 * Side3 * Side2)) * 180 / M_PI;
    	Angle3 = 180 - (Angle1 + Angle2);
    
    
    	edtSASSide3->Text  = FloatToStrF(Side3,  ffFixed, 6, 6);
    	edtSASAngle2->Text = FloatToStrF(Angle2, ffFixed, 6, 2);
    	edtSASAngle3->Text = FloatToStrF(Angle3, ffFixed, 6, 2);
    }
    //---------------------------------------------------------------------------
  9. In the combo box on top of the Object Inspector, select btnSSSCalculate and click Events
  10. Double-click OnClick and implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmCalculations::btnSSSCalculateClick(TObject *Sender)
    {
    	double Angle1 = 0.00, Angle2 = 0.00, Angle3 = 0.00;
    	double Side1  = 0.00, Side2  = 0.00, Side3  = 0.00;
    
    	Side1 = StrToFloat(edtSSSSide1->Text);
    	Side2 = StrToFloat(edtSSSSide2->Text);
    	Side3 = StrToFloat(edtSSSSide3->Text);
    
    	// Here, we use the law of cosines
    	Angle1 = acos(((Side3 * Side3) +
    				   (Side2 * Side2) -
    				   (Side1 * Side1)) /
    				 (2 * Side3 * Side2)) * 180 / M_PI;
    	Angle2 = acos(((Side3 * Side3) +
    				   (Side1 * Side1) -
    				   (Side2 * Side2)) /
    				 (2 * Side3 * Side1)) * 180 / M_PI;
    	Angle3 = 180 - (Angle1 + Angle2);
    
    
    	edtSSSAngle1->Text = FloatToStrF(Angle1, ffFixed, 6, 2);
    	edtSSSAngle2->Text = FloatToStrF(Angle2, ffFixed, 6, 2);
    	edtSSSAngle3->Text = FloatToStrF(Angle3, ffFixed, 6, 2);
    }
    //---------------------------------------------------------------------------
  11. Press F12 to return to the form
  12. Double-click the Close button and implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmCalculations::btnCloseClick(TObject *Sender)
    {
    	Close();
    }
    //---------------------------------------------------------------------------
  13. Press F9 to execute the application
     
    Oblique Triangles
     
    Oblique Triangles
     
    Oblique Triangles
  14. Close the form and return to your programming environment
 
 
 
 

Home Copyright © 2010-2011 FunctionX, Inc.