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.
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 Monthly radio button to access its
OnClick event
- Implement it as follows:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "Calculations.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmCalculations *frmCalculations;
//---------------------------------------------------------------------------
__fastcall TfrmCalculations::TfrmCalculations(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmCalculations::rdoMonthlyClick(TObject *Sender)
{
double Principal = 0.00,
InterestRate = 0.00,
InterestEarned = 0.00;
double FutureValue = 0.00,
RatePerPeriod = 0.00,
Periods = 0;
// Retrieve the value of the principal
Principal = StrToFloat(edtPrincipal->Text);
// Retrieve the interest rate
InterestRate = StrToFloat(edtInterestRate->Text) / 100;
// Get the number of periods
Periods = StrToFloat(edtPeriods->Text);
// These values will make the calculation easier to read
double i = InterestRate / 12;
double n = 12 * Periods;
// Perform the necessary calculations
RatePerPeriod = InterestRate / Periods;
FutureValue = Principal * pow(1 + i, n);
InterestEarned = FutureValue - Principal;
// Display the values in the appropriate text boxes
TVarRec ie[1] = { InterestEarned };
edtInterestEarned->Text = Format(L"%8.2f", ie, 2);
TVarRec fv[1] = { FutureValue };
edtFutureValue->Text = Format(L"%8.2f", fv, 2);
}
//---------------------------------------------------------------------------
- Click the arrow on the top section of the Object Inspector and
select rdoQuarterly
- Click the Events tab
- Double-click the empty box on the right side of OnClick
- Implement the Click event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmCalculations::rdoQuarterlyClick(TObject *Sender)
{
double Principal = 0.00,
InterestRate = 0.00,
InterestEarned = 0.00;
double FutureValue = 0.00,
RatePerPeriod = 0.00,
Periods = 0;
Principal = StrToFloat(edtPrincipal->Text);
InterestRate = StrToFloat(edtInterestRate->Text) / 100;
Periods = StrToFloat(edtPeriods->Text);
double i = InterestRate / 4;
double n = 4 * Periods;
RatePerPeriod = InterestRate / Periods;
FutureValue = Principal * pow(1 + i, n);
InterestEarned = FutureValue - Principal;
TVarRec ie[1] = { InterestEarned };
edtInterestEarned->Text = Format(L"%8.2f", ie, 2);
TVarRec fv[1] = { FutureValue };
edtFutureValue->Text = Format(L"%8.2f", fv, 2);
}
//---------------------------------------------------------------------------
- In the top combo box of the Object Inspector, select rdoSemiannually
- Double-click the box of the OnClick field and implement the Click
event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmCalculations::rdoSemiannuallyClick(TObject *Sender)
{
double Principal = 0.00,
InterestRate = 0.00,
InterestEarned = 0.00;
double FutureValue = 0.00,
RatePerPeriod = 0.00,
Periods = 0;
Principal = StrToFloat(edtPrincipal->Text);
InterestRate = StrToFloat(edtInterestRate->Text) / 100;
Periods = StrToFloat(edtPeriods->Text);
double i = InterestRate / 2;
double n = 2 * Periods;
RatePerPeriod = InterestRate / Periods;
FutureValue = Principal * pow(1 + i, n);
InterestEarned = FutureValue - Principal;
TVarRec ie[1] = { InterestEarned };
edtInterestEarned->Text = Format(L"%8.2f", ie, 2);
TVarRec fv[1] = { FutureValue };
edtFutureValue->Text = Format(L"%8.2f", fv, 2);
}
//---------------------------------------------------------------------------
- In the top combo box of the Object Inspector, select rdoAnnually
- Double-click the box of OnClick and implement the event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmCalculations::rdoAnnuallyClick(TObject *Sender)
{
double Principal = 0.00,
InterestRate = 0.00,
InterestEarned = 0.00;
double FutureValue = 0.00,
RatePerPeriod = 0.00,
Periods = 0;
Principal = StrToFloat(edtPrincipal->Text);
InterestRate = StrToFloat(edtInterestRate->Text) / 100;
Periods = StrToFloat(edtPeriods->Text);
double i = InterestRate;
double n = Periods;
RatePerPeriod = InterestRate / Periods;
FutureValue = Principal * pow(1 + i, n);
InterestEarned = FutureValue - Principal;
TVarRec ie[1] = { InterestEarned };
edtInterestEarned->Text = Format(L"%8.2f", ie, 2);
TVarRec fv[1] = { FutureValue };
edtFutureValue->Text = Format(L"%8.2f", fv, 2);
}
//---------------------------------------------------------------------------
- Press F12 to return to the form
- Double-click the Close button
- Implement its event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmCalculations::btnCloseClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
- To test the form, press F9
- After using it, close the form and save your project
- On the main menu, click File -> Close All
- When asked whether you want to save the project, click Yes
- Click the New Folder button
- Type CompoundInterest1 as the name of the folder
and press Enter twice (to create it and to select it)
- Specify the name of the unit as Calculations and
press Enter
- Specify the name of the project as CompoundInterest
and press Enter
The Columns of Radio Buttons
|
|
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 group box 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.
|
|