Radio Buttons Implementation


Radio (Option) Buttons Implementation

Sometimes, we only want to let the user select one option, not multiple options on a given set of choices or alternatives. In this case, Check boxes are not appropriate to implement, therefore we will use another control object: the Option (Radio) buttons. Like for example an applicant can choose only one to the given options: single, married, or widowed. Or in a situation where a user should choose only one sex status: male or female. In a case like these, we will implement Radio buttons since the user is restricted or limited to choose only one option. Let us design the Option (Radio) Buttons now:

Task
Design the following specified GUI program:
Project name: radio1



When the user clicks the Radio button 3, we will display “Radio button 3 was clicked!” at the Edit box. We will do the same with Radio button 1 and Radio button 3.

Steps
1.       Open the Visual C++ compiler and click the New item in the File menu, opening the New dialog box.

2.       Now select the MFC AppWizard (exe) entry in the New dialog box.

3.       Give the new program the name radio1 in the Project name box.

4.       Click the OK command button to starts the MFC AppWizard then click the option marked “Dialog based” to base our new program on the CDialog class. Click the Finish button to create the new program. Then finally, click the OK command button.

5.       Next, click the Resources tab, open the Dialog folder, and click the entry for our program’s main window IDD_RADIO1_DIALOG. This process opens the dialog editor

**You can erase also the “TODO: Place the dialog controls here.”  Because this message only informs that we have to place the dialog controls (objects) such as command buttons, check boxes (or edit boxes) and others here.

6.       Add a new Radio buttons and a new Edit box to the dialog box. Drag these control objects form the toolbox. You could notice that the dialog editor assigns a caption for each Check box. These captions are Radio 1 for Option button 1, Radio 2 for Option button 2 and Radio 3 for Option button 3. If you want to assign other caption for each radio button, just right-click the radio button, click the Properties item in the pop-up menu that appears, and type in the caption you want.

7.       Now open the ClassWizard now by right-clicking the mouse and find the three radio buttons, IDC_RADIO1, IDC_RADIO2 and IDC_RADIO3. Then connect the event-handlers (methods) to each of these new controls, OnRadio1 (), OnRadio2 () and OnRadio3 () by clicking those objects ID’s value one by one and double-click the BN_CLICKED message in the Message box. This creates the methods (event-handlers) on OnRadio1 (), OnRadio2 () and OnRadio3 (). Click the OK button to accept the suggested method name for each function. After these procedures, we will use again the ClassWizard to connect a member variable to the text in the Edit box, which we will name as m_text. To do this, click the Member Variables tab. Now select the Edit box control, IDC_EDIT1, and click the Add Variable button. Type m_text as the name of the member variable. Make it sure that the Value shows in the Category box and CString in the Variable type box. Finally, click the OK button to close the Add Member variable dialog box. This time, click the Message Maps tab and double-click the OnRadio1 method (located under the Member functions box). This process opens the OnRadio1 () method ( and the OnRadio2 () and OnRadio3 () skeleton methods followed below ).
Now embed the following code:

void CRadio1Dlg::OnRadio1 ()
{
//TODO: Add your control notification handler code here
m_text=”Radio button 1 was clicked!”;
MessageBox(m_text);
UpdateData(false);
}


Above function is the event-handler (method) to be called or execured whe the user clicks the Radio button 1 in the dialog box. Here, we just display the user’s action in the Edit box with the message “Radio button 1 was clicked!”.

8.       Now we will do the same with the OnRadio2 () and OnRadio3 () methods:

void CRadio1Dlg::OnRadio2 ()
{
//TODO: Add your control notification handler code here
m_text=”Radio button 2 was clicked!”;
MessageBox(m_text);
UpdateData(false);
}

void CRadio1Dlg::OnRadio3 ()
{
//TODO: Add your control notification handler code here
m_text=”Radio button 3 was clicked!”;
MessageBox(m_text);
UpdateData(false);
}


               9.   You can now compile and run your program by choosing the Build menu and click the Build  radio1.exe item. Then finally click the ! (exclamation mark) icon to run the program.

                  In this exampe, we add the Message box that is why we have two output messages here, one the Edit box, and one for the Message box. The two similar messages will be displayed one after the other.

Powered by Blogger.
 
;