By default, a Group Box control considers the radio
its holds as a group. This means that, when the application is running,
only one radio button in the group can be selected. During design, you can
add as many radio buttons as necessary but it recommended that you don't
create more than 7 radio buttons in a group. To create the radio button,
click one from the Toolbox and click inside the Group Box control.
- Start Microsoft Visual C++ (any version is fine: Microsoft Visual
C++, Microsoft Visual Studio, Microsoft Visual C++.NET, Microsoft
Visual Studio.NET).
- Create a Dialog Based project called Operations2
Here is the dialog box we will need:
- After making sure the TODO line is deleted, from the Toolbox, click
the Group Box and click on the dialog box to add it. Set its caption
to Calculation
- Add three Static Text controls inside the Calculation group box. Set
their captions to Number &1:, Number &2: and Result,
respectively.
- Add three Edit Box controls to the right side of the existing
labels. Set their IDs to IDC_OPERAND1, IDC_OPERAND2, and
IDC_RESULT, respectively. Optionally, to add a line between the
operands and the result, from the Toolbox, click the Picture control;
on the dialog box, drag a line from the left side between Number 2 and
Result and to the right.
- Add another Group Box control to the right side of the existing one
on the dialog. Set its Caption to Operations
- From the Toolbox, click the Radio Button control and click inside
the Operations group box. Set the radio button's ID to IDC_ADDITION
and set its Caption to &Addition
- Repeat the previous step to add three more radio buttons with the
IDs as IDC_SUBTRACTION, IDC_MULTIPLICATION, and IDC_DIVISION
respectively. Set their captions to &Subtraction,
&Multiplication, and &Division respectively.
- On the main menu, click Layout -> Tab Order. On the dialog, click
the Number 1 edit box to make it number 1. Click the Number 2 edit box
then the Addition radio buttons respectively.
Programming the Radio Buttons |
|
To program your radio buttons, you can consider them
as a group of one control or as each control, which we will do in this
exercise. To consider each radio and to write its own events not as a
group, during design, you should check the Group check box for each radio
button. Then, in the Member Variables property sheet, create a variable
for each radio button. After creating the member variables, get back to
the dialog ion design view and uncheck the Group check box for all radio
buttons; alternatively, you can check only one Check box for one of the
radio buttons. As long as they are included in group container such as a
Group Box or Picture controls, the user will be able to select only one.
- On the dialog, click each radio button and in its Properties window,
check its Group check box.
- On the main menu, click View -> ClassWizard...
- On the MFC ClassWizard dialog, click the Member Variables property
sheet.
- Double-click each control and create the variables as follows:
- Click the Message Maps property sheet.
- Click the Object ID of each radio button and, in the Messages list
box, double-click BN_CLICKED. Accept the function name and click OK.
When you finish, you should have the following functions: OnAddition,
OnSubtraction, OnMultiplication, and OnDivision.
- Click Edit Code and implement the functions as follows (just in case
you wonder why I didn't write any routine for an eventual division by
zero if the user types 0 in the Operand 2 edit box before performing a
division, there are two reasons; 1/Microsoft Visual C++ has the best
Exception Handling mechanism that I have ever seen; it is so
wonderfully crafted it can take care of almost any misbehavior itself;
2/ I left it up to you because the subject of this exercise is on
radio buttons, not exception handling):
void COperationsDlg::OnAddition()
{
UpdateData();
double Number1, Number2, Result;
if( m_Operand1.IsEmpty() )
Number1 = 0;
else
Number1 = atof(m_Operand1);
if( m_Operand2.IsEmpty() )
Number2 = 0;
else
Number2 = atof(m_Operand2);
Result = Number1 + Number2;
m_Result.Format("%.2f", Result);
UpdateData(FALSE);
}
void COperationsDlg::OnSubtraction()
{
UpdateData();
double Number1, Number2, Result;
if( m_Operand1.IsEmpty() )
Number1 = 0;
else
Number1 = atof(m_Operand1);
if( m_Operand2.IsEmpty() )
Number2 = 0;
else
Number2 = atof(m_Operand2);
Result = Number1 - Number2;
m_Result.Format("%.2f", Result);
UpdateData(FALSE);
}
void COperationsDlg::OnMultiplication()
{
UpdateData();
double Number1, Number2, Result;
if( m_Operand1.IsEmpty() )
Number1 = 0;
else
Number1 = atof(m_Operand1);
if( m_Operand2.IsEmpty() )
Number2 = 0;
else
Number2 = atof(m_Operand2);
Result = Number1 * Number2;
m_Result.Format("%.2f", Result);
UpdateData(FALSE);
}
void COperationsDlg::OnDivision()
{
double Number1, Number2, Result;
UpdateData();
if( m_Operand1.IsEmpty() )
Number1 = 0;
else
Number1 = atof(m_Operand1);
if( m_Operand2.IsEmpty() )
Number2 = 0;
else
Number2 = atof(m_Operand2);
Result = Number1 / Number2;
m_Result.Format("%.2f", Result);
UpdateData(FALSE);
}
|
- Very important: Get back to the dialog box design and remove the
check box of the Group check box for each radio control (you can leave
one of them ON but only one or none).
- Test your program
|
|