Check Box Implementation


Check Box Implementation

We will explore now a new control object; and this is the checked boxes. We design a check boxes                      graphical user interface (GUI) to let the user selet one or more options from a nember of given chi=oices or  alternatives such as the ingredients of a pizza (onion rings, chilli and tomato, cheese, mushrooms, bacon and ham, so on).


          Task 
          Design the following specified GUI program
          Program name: check1
  

When the user clicks one of our three check boxes, we indicated which check box the users had clicked in an edit box. Like for example, if Check box 2 was clicked by the user, we will display “Check box 2 was clicked!” at the edit box. We will do the same with the Check box 1 and Check box 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 check1 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_CHECK1_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.       It is easy to add three Check boxes and an Edit box to our Dialog box. Just simply drag those control objects over to the dialog box form the tool box. You could noticed that the dialog editor assigns a caption for each Check box. These captions are Check1 for Check box 1, Check 2 for Check box 2 and Check 3 for Check box 3. If you want to assign other caption for each check box, just right-click the check box, click the Properties item in the pop-up menu that appears and type in the caption you want.

7.       Open the ClassWizard now by right-clicking the mouse and find the three Check boxes, IDC_CHECK1, IDC_CHECK2 and IDC_CHECK3. We can add an event-handlers to these control objects by clicking those Object ID’s values one by one and double-clicking the BN_CLICKED message in the Message box. This creates the methods (event-handlers) on OnCheck1(),OnCheck2() and OnCheck3(). Now 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 OnCheck 1 method (located under the Member functions box). This process opens the OnCHeck1() method.

void CCheck1Dlg:: OnCheck1()
{
//TODO: Add your control notification handler code here
}


Method above is the one called when the user clicks the first check box (Check 1). Clicking the check box changes automatically its logical state form checked (with check mark) to unchecked (blank box) or vice versa (unchecked to checked).

8.       Let us now embed the following code to each method:

void CCheck1Dlg::OnCheck1()
{
//TODO: Add your control notification handler code here
m_text=”Check box 1 was clicked!”;
UpdateData(false);
}

void CCheck1Dlg::OnCheck2()
{
//TODO: Add your control notification handler code here
m_text=”Check box 2 was clicked!”;
UpdateData(false);
}

void CCheck1Dlg::OnCheck3()
{
//TODO: Add your control notification handler code here
m_text=”Check box 3 was clicked!”;
UpdateData(false);
}

9.       You can now compile and run your program by choosing the Build menu and click the Build check1.exe program (project), then click the !(exclamation mark) icon to run the program.

---------- ---------- ---------- ---------- ----------

Note:

      In the example above on Check boxes, the user can click several check boxes as many as three checked check boxes showing at once.
Powered by Blogger.
 
;