|
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 section of the Tool 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.
|
Practical
Learning: Creating Radio Buttons
|
|
- Start a new project with the default form
- To save it, on the Standard toolbar, click the Save All button
- Click the Create New Folder button. Type Operations and press Enter twice
- Click Unit1 to select it. Type Main and press Enter
- Type Operations as the name of the project and press Enter
- On the Standard section of the Tool Palette, click the Panel control

- On the form, click and drag from the top left section to the middle center
section
- On the Object Inspector, click Caption and press Delete to delete the caption of the
panel
- On the Tool Palette, double-click the RadioButton control

- On the Object Inspector, click Caption and type &Addition
- Click Name and type rdoAddition
- Move the new radio button to the top section of and inside the panel
- On the Tool Palette, click the Radio Button
- Click inside the panel
- Click Name and type rdoSubtraction
- Click Caption and type &Subtraction
- In the same way, add another RadioButton control to the panel. Set its Caption to &Multiplication and its Name to rdoMultiplication
- Add one more RadioButton
to the panel
- Set its Name to rdoDivision and its Caption to &Division

- Move the panel to the right section of the form
- Notice that it moves with its “children”. You will redesign the form as follows:

- Add another panel
with four labels
captioned
Number &1:
Number &2:
------------------
Result:
- Add three Edit controls
inside the panel and on the right side of the corresponding labels. These edit boxes will be named
edtNumber1, edtNumber2, and edtResult
- Add another panel to an empty area of the form
- While the new and last panel is selected, click the Align field. Click its arrow and select
alBottom
|
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.
|
Practical
Learning: Implementing Radio Button Events
|
|
- On the form double-click the Addition radio button to access its OnClick event
- Implement it as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmOperations::rdoAdditionClick(TObject *Sender)
{
double Num1;
double Num2;
double Result;
if( edtNumber1->Text == "" )
Num1 = 0;
else
Num1 = StrToFloat(edtNumber1->Text);
if( edtNumber2->Text == "" )
Num2 = 0;
else
Num2 = StrToFloat(edtNumber2->Text);
Result = Num1 + Num2;
edtResult->Text = Result;
}
//---------------------------------------------------------------------------
|
- Click the arrow on the top section of the Object Inspector and select rdoSubtraction
- Click the Events tab
- Double-click the empty box on the right side of OnClick
- Implement the function just like the Click event of the addition but change the operation as follows:
Result = Num1 - Num2;
- On the top combo box of the Object Inspector, select rdoMultiplication
- Double-click the box of the OnClick field
- Implement the Click event like the previous two but change the operation as follows:
Result = Num1 * Num2;
- In the same way, access the OnClick event for the rdoDivision control and implement it as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmOperations::rdoDivisionClick(TObject *Sender)
{
double Num1;
double Num2;
double Result;
if( edtNumber1->Text == "" )
Num1 = 0;
else
Num1 = StrToFloat(edtNumber1->Text);
if( edtNumber2->Text == "" )
{
MessageBox(NULL, "The Number 2 Edit Box should not be empty",
"Algebraic Operations", MB_OK);
edtNumber2->SetFocus();
}
else if( edtNumber2->Text == 0 )
{
MessageBox(0, "Cannot divide by zero",
"Algebraic Operations", MB_OK);
edtNumber2->SetFocus();
}
else
{
Num2 = StrToFloat(edtNumber2->Text);
Result = Num1 / Num2;
edtResult->Text = Result;
}
}
//---------------------------------------------------------------------------
|
- To display the form, press F12
- Double-click the panel on the bottom section of the form to access its OnClick event and implement it as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmOperations::Panel3Click(Tobject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
|
- To test the form, press F9
- After using it, close the form and save your project
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 section of the
Tool 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.
|
Practical
Learning: Using a Radio Group Control
|
|
- Start a new application with its default form
- Save it in a new folder named BodyTag2
- Save the unit as Exercise and save the project as BodyTag
|
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;
}
//---------------------------------------------------------------------------
|
Practical
Learning: Using a Radio Group Control
|
|
- From the Standard section of the Tool Palette, click the RadioGroup control
and click the
left side of the form
- On the Object Inspector, double-click (TStrings) from the Items field
- Type Background and press Enter. Complete the list with Text,
Link, Active Link, and Visited Link

- Click OK
- While the RadioGroup control is still selected, on the Object Inspector, change its properties as follows:
Caption = Body Attributes
ItemIndex = 0
Name = grpBodyAttributes
- Complete the design of the form as follows:
 |
| Control |
Name |
Text/Caption |
Other Properties |
| Form |
frmMain |
Body Tag Formatter |
BorderStyle: bsDialog
Position: poScreenCenter |
| RadioGroup |
grpBodyAttributes |
Body Attributes |
ItemIndex: 0 |
| Edit |
edtBackground |
#000000 |
|
| Edit |
edtText |
#0000FF |
|
| Edit |
edtLink |
#FF0000 |
|
| Edit |
edtALink |
#008000 |
|
| Edit |
edtVLink |
#800000 |
|
| Label |
|
Preview ___________ |
|
| Panel |
pnlPreview |
|
Color: clWhite |
| Label |
|
R |
|
| ScrollBar |
scrRed |
|
Kind: sbVertical
Max: 255
Position: 255 |
| Label |
|
G |
|
| ScrollBar |
scrGreen |
|
Kind: sbVertical
Max: 255
Value: 255 |
| Label |
|
B |
|
| ScrollBar |
scrBlue |
|
Kind: sbVertical
Max: 255
Value: 255 |
| BitBtn |
|
&Close |
Kind: bkClose |
| Memo |
mmoPreview |
|
Lines: Nothing |
| Edit |
edtPreviewText |
Body tag formatter and colorizer |
Font->Color: clBlue
ReadOnly: true |
| Edit |
edtPreviewLink |
Sample text as link |
Font->Color: clRed
ReadOnly: true |
| Edit |
edtPreviewALink |
Active link that is being visited |
Font->Color: clGreen
ReadOnly: true |
| Edit |
edtPreviewVLink |
This link has been already been visited |
Font->Color: clMaroon
ReadOnly: true |
| GroupBox |
|
Hexadecimal |
|
| Label |
|
Red: |
|
| Edit |
edtHexaRed |
00 |
|
| Label |
|
Green: |
|
| Edit |
edtHexaGreen |
00 |
|
| Label |
|
Blue: |
|
| Edit |
edtHexaBlue |
00 |
|
| GroupBox |
|
Numeric |
|
| Label |
|
Red: |
|
| Edit |
edtRed |
00 |
|
| Label |
|
Green: |
|
| Edit |
edtGreen |
00 |
|
| Label |
|
Blue: |
|
| Edit |
edtBlue |
00 |
|
| BitBtn |
btnCopy |
Cop&y |
|
| Edit |
edtBody |
<body bgcolor="#FFFFFF" text="#0000FF" link="#FF0000" alink="#008000" vlink="#800000"> |
|
- Access each edit box in the lower-left memo and set its BorderStyle
property to bsNone
- In the header file of the form, declare the following private variables:
private: // User declarations
AnsiString HexBG, HexText, HexLink, HexALink, HexVLink;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
|
- In the ClassExplorer window, right-click TForm1 and click New Method...
- Set the method options as follows:
Method Name: ApplyColor
Function Result: void
Visibility: private
Directives: __fastcall
- Click OK and implement the method as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ApplyColor()
{
//TODO: Add your source code here
// Retrieve the current hexadecimal colors from their Edit controls
HexBG = edtBackground->Text;
HexText = edtText->Text;
HexLink = edtLink->Text;
HexALink = edtALink->Text;
HexVLink = edtVLink->Text;
// Get the integral position of each ScrollBar control
int Red = 255 - scrRed->Position;
int Green = 255 - scrGreen->Position;
int Blue = 255 - scrBlue->Position;
// Display the position of each ScrollBar
// in its corresponding Edit control
edtRed->Text = Red;
edtGreen->Text = Green;
edtBlue->Text = Blue;
// Get the hexadecimal equivalent of each ScrollBar control
AnsiString HexRed = IntToHex(Red, 2);
AnsiString HexGreen = IntToHex(Green, 2);
AnsiString HexBlue = IntToHex(Blue, 2);
// Display the hexadecimal value in the appropriate Edit controls
edtHexaRed->Text = HexRed;
edtHexaGreen->Text = HexGreen;
edtHexaBlue->Text = HexBlue;
// Change the color of the Preview panel
// according to the values of the ScrollBar positions
pnlPreview->Color = TColor( RGB(Red, Green, Blue) );
// Get the position of each ScrollBar control
// Create a hexadecimal color starting with #
// And display the color in the appropriate Edit control
switch( grpBodyAttributes->ItemIndex )
{
case 0:
edtBackground->Text = "#" +
IntToHex(Red, 2) +
IntToHex(Green, 2) +
IntToHex(Blue, 2);
mmoPreview->Color = pnlPreview->Color;
edtPreviewText->Color = pnlPreview->Color;
edtPreviewLink->Color = pnlPreview->Color;
edtPreviewALink->Color = pnlPreview->Color;
edtPreviewVLink->Color = pnlPreview->Color;
HexBG = edtBackground->Text;
break;
case 1:
edtText->Text = "#" +
IntToHex(Red, 2) +
IntToHex(Green, 2) +
IntToHex(Blue, 2);
edtPreviewText->Font->Color = TColor( RGB(Red, Green, Blue) );
HexText = edtText->Text;
break;
case 2:
edtLink->Text = "#" +
IntToHex(Red, 2) +
IntToHex(Green, 2) +
IntToHex(Blue, 2);
edtPreviewLink->Font->Color = TColor( RGB(Red, Green, Blue) );
HexLink = edtLink->Text;
break;
case 3:
edtALink->Text = "#" +
IntToHex(Red, 2) +
IntToHex(Green, 2) +
IntToHex(Blue, 2);
edtPreviewALink->Font->Color = TColor( RGB(Red, Green, Blue) );
HexALink = edtALink->Text;
break;
case 4:
edtVLink->Text = "#" +
IntToHex(Red, 2) +
IntToHex(Green, 2) +
IntToHex(Blue, 2);
edtPreviewVLink->Font->Color = TColor( RGB(Red, Green, Blue) );
HexVLink = edtVLink->Text;
break;
}
// Update the contents of the bottom Edit control
edtBody->Text = "<body bgcolor=\"" +
HexBG +
"\" text=\"" +
HexText +
"\" link=\"" +
HexLink +
"\" alink=\"" +
HexALink +
"\" vlink=\"" +
HexVLink +
"\">";
}
//---------------------------------------------------------------------------
|
- Double-click each scroll bar and implement their OnChange event as
follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::scrRedChange(TObject *Sender)
{
ApplyColor();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::scrGreenChange(TObject *Sender)
{
ApplyColor();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::scrBlueChange(TObject *Sender)
{
ApplyColor();
}
//---------------------------------------------------------------------------
|
- Test the application and return to C++ Builder
- When the user clicks a radio button from the Body Attributes group box,
we need to display its color on the Preview panel. When a particular button
is clicked, we will
retrieve the color of its font from the Body text box, translate that color
into red, green, and blue values, and then use those values to automatically
update the scroll bars and the edit boxes. While we are at it, we also need
to update the corresponding text box in the Body Attributes group box. Since
this functionality will be used by all radio buttons in the group, we will
use a global function to which we can pass two variables.
When the user clicks a particular radio button, that button is represented
by a text box in the lower-left Body section. We need to get the color of
that edit box and pass it to our function. Since the clicked radio button
has a corresponding text box in the Body Attributes group box, we need to
change/update that value with the hexadecimal value of the first argument.
Therefore, we will pass a String argument to our function.
In the header file of the form, declare a method as follows:
private: // User declarations
AnsiString HexBG, HexText, HexLink, HexALink, HexVLink;
void __fastcall ApplyColor();
void __fastcall ClickOption(TColor Clr, AnsiString Result);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
|
- In the source code of the form, implement the function as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ClickOption(TColor Clr, AnsiString Result)
{
long lngColor;
int Red, Green, Blue;
pnlPreview->Color = Clr;
lngColor = Clr;
Red = 255 - (lngColor % 256);
Green = 255 - ((lngColor / 256) % 256);
Blue = 255 - (lngColor / 65536);
scrRed->Position = Red;
scrGreen->Position = Green;
scrBlue->Position = Blue;
edtRed->Text = Red;
edtGreen->Text = Green;
edtBlue->Text = Blue;
edtHexaRed->Text = IntToHex(Red, 2);
edtHexaGreen->Text = IntToHex(Green, 2);
edtHexaBlue->Text = IntToHex(Blue, 2);
Result = "#" + IntToHex(Red, 2) + IntToHex(Green, 2) + IntToHex(Blue, 2);
}
//---------------------------------------------------------------------------
|
- Save all
|
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.
|
Practical
Learning: Implementing a Radio Group Control
|
|
- Double-click the grpBodyAttributes RadioGroup control and implement its
OnClick as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmMain::grpBodyAttributesClick(TObject *Sender)
{
// If the user clicks a button from the RadioGroup control
// find out what button the user clicked
// set color of the panel to that of the radio button that was clicked
TColor BGColor = mmoPreview->Color;
mmoPreview->Color = BGColor;
edtPreviewText->Color = BGColor;
edtPreviewLink->Color = BGColor;
edtPreviewALink->Color = BGColor;
edtPreviewVLink->Color = BGColor;
switch(grpBodyAttributes->ItemIndex)
{
case 0:
ClickOption(mmoPreview->Color, edtBackground->Text);
HexBG = edtBackground->Text;
break;
case 1:
ClickOption(edtPreviewText->Font->Color, edtText->Text);
HexText = edtText->Text;
break;
case 2:
ClickOption(edtPreviewLink->Font->Color, edtLink->Text);
HexLink = edtLink->Text;
break;
case 3:
ClickOption(edtPreviewALink->Font->Color, edtALink->Text);
HexALink = edtALink->Text;
break;
case 4:
ClickOption(edtPreviewVLink->Font->Color, edtVLink->Text);
HexVLink = edtVLink->Text;
break;
}
}
//---------------------------------------------------------------------------
|
- Save and test the application then return to C++ Builder
- Double-click the Copy button and implement it as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCopyClick(TObject *Sender)
{
edtBody->SelectAll();
edtBody->CopyToClipboard();
}
//---------------------------------------------------------------------------
|
- Test the application

|
The Database Radio Group Control |
|
When creating a database object such as a table, you may
provide a column that allows a user to set a single value from a small list of
values. For example, you can create column that would be used to specify the
gender of an applicant. Instead of letting the user enter either Female or Male,
you can create the options yourself and then let the user only select the
desired one. To implement this scenario in a Windows application, we saw that
you can use radio buttons.
To support radio buttons in a database, the VCL provides the
DBRadioGroup control.The DBRadioGroup control primarily behaves like a
RadioGroup control. In fact, both descend from the TCustomRadioGroup. Therefore,
to use a DBRadioGroup in your application, click it from the Data Controls
section of the Tool Palette and click the desired container.
Like the RadioGroup control, the radio buttons of a
DBRadioGroup object are represented by the Items property that is a list
based on the TStrings class. This also means that, when creating a table
for your application, you can create a string-based column and you would later
on associate it to a DBRadioGroup object. You probably already know that if you
create a text-based column, it can have just any value. If you intend to
associated such a column to a DBRadioGroup, it is your responsibility to make
sure that the list of possible strings for this column should (must) be limited.
In fact, when planning your table, you should make sure that the user will not
be allowed to create new options. For example, if you create a column that you
anticipate to have a fixed list of strings, such as Female and Male, keep in
mind that you may not allow the user to add new strings such as Unknown or
Hermaphrodite.
|
Practical
Learning: Introducing the DB Radio Group Control
|
|
- To start a database application, on the main menu, click Tools ->
Database Desktop
- On the main menu of the Database Desktop, click File -> New ->
Table...
- Accept the Paradox 7 option and click OK
- Create the fields as follows:
| Field Name |
Type |
Size |
Key |
| LoanPrepID |
+ |
|
* |
| PreparedBy |
A |
50 |
|
| PreparedFor |
A |
50 |
|
| Description |
M |
240 |
|
| Principal |
A |
12 |
|
| InterestRate |
A |
12 |
|
| Periods |
A |
10 |
|
| CompoundFrequency |
A |
20 |
|
| InterestEarned |
A |
12 |
|
| AmountPaid |
A |
12 |
|
- To save the table, click the Save As button
- Save the name to LoanPreparation and select the BCDEMOS alias

- Click Save
- Close the Database Desktop
- In Borland C++ Builder, create a new Application
- Set the form's Caption to Watts A Loan
- Save the application in a new folder named WattsALoan1
- Save the unit as Exercise and the project as WattsALoan
- In the BDE section of the Tool Palette, click Table and click the
form
- In the Object Inspector, set its properties as follows:
DatabaseName: BCDEMOS
Name: tblLoanPrep
TableName: LoanPreparation
- In the Data Access section of the Tool Palette, click DataSource and
click the form
- In the Object Inspector, set its properties as follows:
DataSet: tblLoanPrep
Name: dsLoanPrep
- Design of the form as follows:
 |
| Control |
Name |
Caption |
Other Properties |
| Form |
frmMain |
Watts A Loan |
BorderIcons: [biSystemMenu,biMinimize]
Position: poScreenCenter
ShowHint: true |
| GroupBox |
|
Preparation |
|
| Label |
|
Prepared By: |
|
| DBEdit |
dbePreparedBy |
|
DataSource: dsLoanPrep
DataField: PreparedBy |
| Label |
|
Prepared For: |
|
| DBEdit |
dbePreparedFor |
|
DataSource: dsLoanPrep
DataField: PreparedFor |
| Label |
|
Description: |
|
| DBEdit |
dbeDescription |
|
DataSource: dsLoanPrep
DataField: Description |
| GroupBox |
|
Values |
|
| Label |
|
Principal |
|
| DBEdit |
dbePrincipal |
|
DataSource: dsLoanPrep
DataField: Principal |
| Label |
|
Interest Rate: |
|
| DBEdit |
dbeInterestRate |
|
DataSource: dsLoanPrep
DataField: InterestRate |
| Label |
|
% |
|
| Label |
|
Periods: |
|
| DBEdit |
dbePeriods |
|
DataSource: dsLoanPrep
DataField: Periods |
|
- Save all
|
Characteristics of the DB Radio Group Control |
|
If you add a DBRadioGroup control to your application, at
design time, you should create a list of the available options yourself. To do
this, display the String List Editor from double-clicking the Strings field of
the Items property in the Object Inspector.
As described above, each item of the radio buttons of a
DBRadioGroup is a member of a TStrings collection and therefore is of
type AnsiString. Such an item is represented by the Value property
of this control. This means that, when the user selects a radio button, its Value,
not its TStrings::ItemIndex property, gets stored in the corresponding
column of the table. This also implies the the value of the record is saved as a
string. This value is the caption of the radio button, as you would have created
it using the Items property. It is important to know that you can use the
properties of the TStrings class to identity the radio button that the
user had clicked but when the record is saved, it is the Value, which is
a string of type AnsiString, which is the caption of the radio button,
that is saved in the field, not the ItemIndex.
While each radio button holds Value string, the group of
values of the radio buttons is stored in the Values property of the DBRadioGroup
control.
|
Practical
Learning: Using a DB Radio Group Control
|
|
- In the Tool Palette, click the DBRadioGroup control and click the
empty lower-right section of the form
- While the new control is still selected, in the Object Inspector, click
Items and click its ellipsis button
- Complete the list with Monthly, Quarterly, Semiannually, and Annually

- Click OK
- Complete the design of the form as follows:
 |
| Control |
Name |
Caption |
Other Properties |
| DBRadioGroup |
grpFrequency |
Compound Frequency |
DataSource: dsLoanPrep
DataField: CompoundFrequency |
| GroupBox |
|
Results |
|
| Label |
|
Interest Earned: |
|
| DBEdit |
dbeInterestEarned |
|
DataSource: dsLoanPrep
DataField: InterestEarned |
| Label |
|
Amount Paid: |
|
| DBEdit |
dbeAmountEarned |
|
|
| BitBtn |
btnCalculate |
Calculate |
|
| DBNavigator |
|
|
DataSource: dsLoanPrep |
| BitBtn |
|
|
Kind: bkClose |
|
- Double-click the Calculate button and implement its event as follows:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "Exercise.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnCalculateClick(TObject *Sender)
{
double Principal, InterestRate, InterestEarned,
AmountPaid;
int Periods, CompoundType;
Principal = this->dbePrincipal->Text.ToDouble();
InterestRate = this->dbeInterestRate->Text.ToDouble() / 100;
switch( this->grpFrequency->ItemIndex )
{
case 0:
CompoundType = 12;
break;
case 1:
CompoundType = 4;
break;
case 2:
CompoundType = 2;
break;
case 3:
CompoundType = 1;
break;
}
Periods = this->dbePeriods->Text.ToInt();
double i = InterestRate / CompoundType;
int n = CompoundType * Periods;
AmountPaid = Principal * pow(1 + i, n);
InterestEarned = AmountPaid - Principal;
this->dbeInterestEarned->Text =
FloatToStrF(InterestEarned, ffFixed, 8, 2);
this->dbeAmountPaid->Text = FloatToStrF(AmountPaid,
ffFixed, 8, 2);
}
//---------------------------------------------------------------------------
|
- Return to the form.
Set the table's Active property to true

- Execute the application and create a loan as follows and click the
Calculate button:

- Save the Post Edit button

- Click the Close button
|
|