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. |
|
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:
To visually create a radio group, in the Standard section of the Tool Palette, click the TRadioGroup control 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);
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; } //---------------------------------------------------------------------------
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"); } //---------------------------------------------------------------------------
|
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.
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.
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.
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; } //---------------------------------------------------------------------------
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.
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|