Visual C++ Radio Buttons

A radio button is a Windows control made of two sections: a round box O and a label. The label informs the user as to what the control is used for. The user makes his or her decision by selecting or clicking the round box 8. In practical usage, a radio button is accompanied by one or more other radio buttons that appear and behave as a group. The user decides which button is valid by selecting only one of them. When the user clicks one button, its round box fills with a (big) dot: 8. When one button in the group is selected, the other round buttons of the (same) group are empty. The user can select another button by clicking a different choice, which empties the previous selection. This technique of selecting is referred to as mutually-exclusive. 

Radio Buttons


Designing Radio Buttons

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.

  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 Operations1
    Here is the dialog box we will need:
  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, 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):
  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. 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.

  1. 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.
  2. Click the Member Variables property page. Double-click each control and add the following variables:
    MFC ClassWizard
  3. Click OK
  4. On the Workspace, click the ClassView property sheet and expand the Operations1 classes folder. Also expand the COperationsDlg folder.
  5. Right-click COperations1Dlg folder and click Add Member Function...
  6. Set the Function Type to void and the Function Name to Calculate
  7. 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.
  8. Implement the function as follows:
    void COperations1Dlg::Calculate()
    	double Number1, Number2, Result;
    	// Evaluate the content of each operand edit box
    	if( m_Operand1.IsEmpty() )
    		Number1 = 0;
    		Number1 = atof(m_Operand1);
    	if( m_Operand2.IsEmpty() )
    		Number2 = 0;
    		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
    	case 0:
    		Result = Number1 + Number2;
    	case 1:
    		Result = Number1 - Number2;
    	case 2:
    		Result = Number1 * Number2;
    	case 3:
    		Result = Number1 / Number2;
    	// Now that a result is available, display it
    	// in the Result edit box
    	m_Result.Format("%.2f", Result);
  9. 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
  10. Press Ctrl + W to get back to the MFC ClassWizard.
  11. 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
  12. 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() 
    void COperations1Dlg::OnSubtraction() 
    void COperations1Dlg::OnMultiplication() 
    void COperations1Dlg::OnDivision() 
  13. 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
  14. Test your program

Home Copyright © 2003-2005 FunctionX