Home

Option-Based Controls

 
 

Introduction

This application is used to illustrate different ways of using or implement the check box controls, including its properties, methods, and events.

Practical Learning Practical Learning: Introducing Check Boxes

 

  1. Start a new project with its default form
  2. Save it in a new folder named FastFood1
  3. Save the unit as Exercise and save the project as FastFood
  4. Open Image Editor. Design a 32 x 32 and 16 x 16 icon as follows:
     
  5. Save it as FastFood in the folder of the current project
  6. Create a new 16 x 16 size bitmap and design it as follows:
     
  7. Save it as Ingredients in the folder of the current project
  8. In the project options (Project -> Options), access the Application tab. Set the Title to Fast Food Corner and sect the icon to the above FastFood icon
  9. Change the form’s following properties:
    BorderStyle: bsDialog
    Caption: Fast Food Restaurant – Customer Menu
    Name: frmMain
    Position: poScreenCenter
  10. Design the dialog box as follows:
     
    Control Name Caption Other Properties
    GroupBox   Bread  
    RadioButton rdoBun B&un Alignment: taLeftJustify
    RadioButton rdoRoll &Roll Alignment: taLeftJustify
    Checked: true
    Group Box   Meat  
    Radio Button rdoBeefPatty Bee&f Patty Alignment: taLeftJustify
    Checked: true
    Radio Button rdoGrilledChicken &Grilled Chicken Alignment: taLeftJustify
    Radio Button rdoChickedBreast C&hicken Breast Alignment: taLeftJustify
    GroupBox   Ingredients  
  11. Save All
  12. On the Standard toolbar, click the New button . In the New Items dialog box, click Dialogs and double-click Standard Dialog (Vertical)
  13. Save it as Ingredients
  14. Change its Name to dlgIngredients
  15. Change its Caption to Ingredients Selection
  16. Design the dialog box as follows:
     
    Control Name Caption Other Properties
    CheckBox chkLettuce &Lettuce Alignment: taLeftJustify
    Checked: true
    CheckBox chkOnion &Onion Alignment: taLeftJustify
    CheckBox chkTomato &Tomato Alignment: taLeftJustify
    Checked: true
    CheckBox chkPickles &Pickles Alignment: taLeftJustify
  17. Display the main form (View -> Forms… -> frmMain -> OK)
  18. Complete its design as follows:
     
    Control Name Caption Other Properties
    CheckBox chkRegulars &Regulars Alignment: taLeftJustify
    AllowGrayed: true
    CheckBox chkSweetener &Sweetener Alignment: taLeftJustify
    Checked: true
    CheckBox chkCheese Ch&eese Alignment: taLeftJustify
    CheckBox chkBacon B&acon Alignment: taLeftJustify
    GroupBox   Options  
    BitBtn btnIngredients &Ingredients Glyph: Ingredients
    RadioButton rdoMayonnaise &Mayonnaise Alignment: taLeftJustify
    Checked: true
    RadioButton rdoKetchup &Ketchup Alignment: taLeftJustify
    RadioButton chkMustard Mus&tard Alignment: taLeftJustify
    Bevel     Shape: bsFrame
    BitBtn     Kind: bkClose
    Label   Total Price:  
    Edit edtTotalPrice $2.35  
  19. On the main form, double-click the Ingredients button and implement its OnClick() event as follows:
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Exercise.h"
    #include "Ingredients.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TfrmMain *frmMain;
    //---------------------------------------------------------------------------
    __fastcall TfrmMain::TfrmMain(TComponent* Owner)
    	: TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::btnIngredientsClick(TObject *Sender)
    {
    	dlgIngredients->ShowModal();
    }
    //---------------------------------------------------------------------------
  20. Test the application. Close it and return to Bcb
  21. Save All
  22. If the user completely removes the check mark on the Sweetener check box, this suggests that the customer does not want this item on the sandwich. Consequently, the radio buttons in the Options group should be disabled. When the user clicks a check box, whether the control was already checked or not, the OnClick() event fires. Therefore, in this case, the first thing you should do it to check the state of the check button and then implement a behavior accordingly.
    Display the main form and double-click the Sweetener check control
  23. Implement its OnClick event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::chkSweetenerClick(TObject *Sender)
    {
    	// If the sweetener check box is not checked
    	if( chkSweetener->Checked == False )
    	{
    		// Disable the Options radio buttons
    		rdoMayonnaise->Enabled = False;
    		rdoKetchup->Enabled = False;
    		rdoMustard->Enabled = False;
    	}
    	else
    	{
    		// Otherwise, enable the Options radio buttons
    		rdoMayonnaise->Enabled = True;
    		rdoKetchup->Enabled = True;
    		rdoMustard->Enabled = True;
    	}
    }
    //---------------------------------------------------------------------------
  24. To keep track of the user’s selection of ingredients, we will use four global variables that each represents a check box from the Ingredients dialog.
    In the header file of the main form, declare four private Boolean variables as follows:
     
    private:
    	Boolean bLettuce, bOnion, bTomato, bPickles;
  25. In the constructor of the main form, initialize the variables with a false value each:
     
    //---------------------------------------------------------------------------
    __fastcall TfrmMain::TfrmMain(TComponent* Owner)
    	: TForm(Owner)
    {
    	bLettuce = False;
    	bOnion = False;
    	bTomato = False;
    	bPickles = False;
    }
    //---------------------------------------------------------------------------
  26. When the user clicks the Regulars check box, we will update the global variables appropriately.
    On the form, double-click the Regulars check box and implement its OnClick() event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::chkRegularsClick(TObject *Sender)
    {
    	// If the Regulars check box is completely unchecked
    	if( chkRegulars->State == cbUnchecked )
    	{
    		// Set the global Boolean variables to false each
    		bLettuce = False;
    		bOnion = False;
    		bTomato = False;
    		bPickles = False;
    	}
    	// If the Regulars check box is completely checked
    	else if( chkRegulars->State == cbChecked )
    	{
    		// Set the global Boolean variables to true each
    		bLettuce = True;
    		bOnion = True;
    		bTomato = True;
    		bPickles = True;
    	}
    	// Otherwise, refer to the state of the check boxes
    	// from the Ingredients dialog box. Whatever they are
    	else
    	{
    		bLettuce = dlgIngredients->chkLettuce->Checked;
    		bOnion = dlgIngredients->chkOnion->Checked;
    		bTomato = dlgIngredients->chkTomato->Checked;
    		bPickles = dlgIngredients->chkPickles->Checked;
    	}
    }
    //---------------------------------------------------------------------------
  27. When the Regulars check box is not checked at all, no basic ingredient is selected. When this control is checked, all ingredients will be added to the sandwich. If at least one ingredient is selected and at least one ingredient is not selected, the Regulars check box should appear grayed.
    When the user clicks the Ingredients button, we will display the Ingredients Selection dialog box and allow the user to select the ingredients. If the user clicks OK to dismiss the dialog box, we will apply the above scenario.
    On the form, double-click the Ingredients button and change its OnClick event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::btnIngredientsClick(TObject *Sender)
    {
    	// Before displaying the dialog box, synchronize its
    	// check boxes with the global variables
    	dlgIngredients->chkLettuce->Checked = bLettuce;
    	dlgIngredients->chkOnion->Checked = bOnion;
    	dlgIngredients->chkTomato->Checked = bTomato;
    	dlgIngredients->chkPickles->Checked = bPickles;
    
    	// Call the Ingregients Selection dialog box for the user
    	// If the user clicked OK when closing the dialog box,
    	dlgIngredients->ShowModal();
    
    	if( dlgIngredients->ModalResult == mrOk )
    	{
    		// If the user clicks OK, update the global values of the check boxes
    		bLettuce = dlgIngredients->chkLettuce->Checked;
    		bOnion = dlgIngredients->chkOnion->Checked;
    		bTomato = dlgIngredients->chkTomato->Checked;
    		bPickles = dlgIngredients->chkPickles->Checked;
    
    		// if no check box is checked
    		if( (bLettuce == False) &&
    		    (bOnion == False) &&
    		    (bTomato == False) &&
    		    (bPickles == False) )
    			chkRegulars->State = cbUnchecked; // then uncheck this one
    		// if all check boxes are checked
    		else if( (bLettuce == True) &&
    			 (bOnion == True) &&
    			 (bTomato == True) &&
    			 (bPickles == True) )
    			chkRegulars->State = cbChecked; // then check this one
    		else // if at least one check box is checked and at one is unchecked
    			chkRegulars->State = cbGrayed; // then set this one as indeterminate
    	}
    	// If the user clicked Cancel, don't do nothing
    }
    //---------------------------------------------------------------------------
  28. Now we can calculate the price of the sandwich.
    In the header file of the main form, declare a private member function of type void __fastcall named EvaluatePrice

  29. Implement the method as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::EvaluatePrice()
    {
    	//TODO: Add your source code here
    	double PriceBread, PriceMeat,
    	PriceCheese, PriceBacon, TotalPrice;
    
    	// The price of bread is $0.85
    	PriceBread = 0.85;
    
    	// To get the price of the meat, find out what button
    	// is selected in the Meat group box
    	if( rdoBeefPatty->Checked == True )
    		// The beef patty is $1.50
    		PriceMeat = 1.50;
    	else if( rdoGrilledChicken->Checked == True ||
    		 rdoChickenBreast->Checked == True )
    		// Cheicken breast and grilled chicken are $1.80 each
    		PriceMeat = 1.80;
    	else // Just in case
    		PriceMeat = 0.00;
    
    	// There is no extra cost for the Regular ingredients
    	// and nothing to add for the sweetener
    
    	// On the other hand,
    	// if the customer wants cheese, $0.30 is added to the price
    	if( chkCheese->Checked == True )
    		PriceCheese = 0.30;
    	else // otherwise, the customer don't want no cheese
    		PriceCheese = 0.00;
    
    	// If the customer wants bacon, $0.45 is added to the price
    	if( chkBacon->Checked == True )
    		PriceBacon = 0.45;
    	else
    	PriceBacon = 0.00;
    
    	// Now, we can calculte the total price
    	TotalPrice = PriceBread + PriceMeat + PriceCheese + PriceBacon;
    	edtTotalPrice->Text = FloatToStrF(TotalPrice, ffCurrency, 8, 2);
    }
    //---------------------------------------------------------------------------
  30. To update the price and its display live whenever the user makes a new selection, double-click the following controls: Beef Patty, Grilled Chicken, Chicken Breast, Cheese, and Bacon
  31. In the body of each, simply call the above EvaluatePrice() method:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::rdoBeefPattyClick(TObject *Sender)
    {
    	EvaluatePrice();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::rdoGrilledChickenClick(TObject *Sender)
    {
    	EvaluatePrice(); 
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::rdoChickedBreastClick(TObject *Sender)
    {
    	EvaluatePrice();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::chkCheeseClick(TObject *Sender)
    {
    	EvaluatePrice();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::chkBaconClick(TObject *Sender)
    {
    	EvaluatePrice();
    }
    //---------------------------------------------------------------------------
  32. Test the application:
     
    The Fast Food Restaurant Application
  33. Close it and return to Bcb
  34. Heighten the form and add the following controls to the top section

    The Fast Food Application Improvement 

     
    Control Name Caption/Text Other Properties
    Label   Processed By:  
    Edit edtClerk    
    Label   Order Date:  
    MaskEdit edtOrderDate   EditMask: !99/99/0000;1;_
  35. Save All
 
Home Copyright © 2005-2016, FunctionX