Practical Learning Logo

Visual C++ Radio Buttons

 
 

Designing Radio Buttons

In this exercise, we will see how to implement the event of each radio button although the radio buttons belong in a group. The difference with this programming of radio buttons comes from their design.

Although considered a control of its own right, a radio button almost stands by itself. Radio buttons go by a group, which allows the user to select one from the group. To configure radio buttons in a group, you should include them in a control that would "hold" them as a group. The most available control to simulate this is a Group Box control. Alternatively, you can also use a Picture control. For this exercise, we will use a Group Box control to host the radio buttons.

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.

  1. Start Microsoft Visual C++ (any version is fine: Microsoft Visual C++, Microsoft Visual Studio, Microsoft Visual C++.NET, Microsoft Visual Studio.NET).
  2. Create a Dialog Based project called Operations2

    Here is the dialog box we will need:
     
    Operations
  3. 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
  4. Add three Static Text controls inside the Calculation group box. Set their captions to Number &1:, Number &2: and Result, respectively.
  5. 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.
  6. Add another Group Box control to the right side of the existing one on the dialog. Set its Caption to Operations
  7. 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
  8. 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.
  9. 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.

  1. On the dialog, click each radio button and in its Properties window, check its Group check box.
  2. On the main menu, click View -> ClassWizard...
  3. On the MFC ClassWizard dialog, click the Member Variables property sheet.
  4. Double-click each control and create the variables as follows:
     
    MFC ClassWizard
  5. Click the Message Maps property sheet.
  6. 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.
  7. 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);
    }
  8. 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).
  9. Test your program
 
 

Home Copyright © 2002-2007 FunctionX, Inc. FunctionX