|
Although considered a control of its own right, a
radio button almost never stands by itself: radio buttons go by a group, which
allows the user to select only one (from the same group). To configure radio buttons
in a group, you should include them in a control that would
"hold" them, as a group. The most common control to simulate
this is a Group Box.
By default, a Group Box control considers the radio
buttons it holds as a group. This means that, when the application is running,
only one radio button in the group can be selected.
After adding the radio buttons to an MFC (or Win32)
application, make sure that only
one of them has its Group property set to true. The radio button that has the
Group property set to true should have an easily recognizable name because the
other controls will be managed from that one.
- 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 Operations1
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, set its Caption to &Addition,
and check its Group check box (make sure it displays a check mark; in
the .NET environment select True in the Group combo box):
- 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. Make sure each
one of these last three radio buttons has its Group check box
unchecked (there must not be a check mark in it) or set to False.
|
Programming the Group of Radio Buttons |
|
To program the radio buttons that are in a group, you
only need the one that has its Group check box ON. In the MFC ClassWizard,
create that radio button as an integer. The radio buttons in a group are
considered an array of integers. The first radio button has a subscript of
0, the second is 1, etc. When the user clicks one of the radio buttons,
you can use a condition statement (if or switch) to find out
which one is selected or has focus.
- In Microsoft Visual C++ or Studio, press Ctrl + W to access the MFC
ClassWizard. In the .NET, right-click the Number 1 edit box and click
Add Variable.
- Click the Member Variables property page. Double-click each control
and add the following variables:
- Click OK
- On the Workspace, click the ClassView property sheet and expand the
Operations1 classes folder. Also expand the COperationsDlg folder.
- Right-click COperations1Dlg folder and click Add Member Function...
- Set the Function Type to void and the Function Name to Calculate
- Click the Private radio button (since we don't intend to let this
dialog communicate with an external control, we can make this function
private) and click OK.
- Implement the function as follows:
void COperations1Dlg::Calculate()
{
double Number1, Number2, Result;
UpdateData();
// Evaluate the content of each operand edit box
if( m_Operand1.IsEmpty() )
Number1 = 0;
else
Number1 = atof(m_Operand1);
if( m_Operand2.IsEmpty() )
Number2 = 0;
else
Number2 = atof(m_Operand2);
// Find out what radio button the user clicked
// This is done considering that the radio buttons
// are stored in an array.
// The selected radio button can be identified by
// its ordinal position based on the m_Operation integer
switch(m_Operation)
{
case 0:
Result = Number1 + Number2;
break;
case 1:
Result = Number1 - Number2;
break;
case 2:
Result = Number1 * Number2;
break;
case 3:
Result = Number1 / Number2;
break;
}
// Now that a result is available, display it
// in the Result edit box
m_Result.Format("%.2f", Result);
UpdateData(FALSE);
}
|
- From the ResourceView tab of the Workspace, display the dialog box
and uncheck the Group check box of the
Addition radio button to make it empty
- Press Ctrl + W to get back to the MFC ClassWizard.
- Click the Message Maps property sheet. Click the Object ID of each
radio control then, in the Messages list box, double-click BN_CLICKED,
accept the function name, and click OK
- Click Edit Code and simply call the Calculate function every time
the user clicks one of the radio buttons. Implement them as follows:
void COperations1Dlg::OnAddition()
{
Calculate();
}
void COperations1Dlg::OnSubtraction()
{
Calculate();
}
void COperations1Dlg::OnMultiplication()
{
Calculate();
}
void COperations1Dlg::OnDivision()
{
Calculate();
}
|
- From the ResourceView tab of the Workspace, display the dialog box
and check (put a check mark on) the Group check box of the
Addition radio button
- Test your program
|
|