Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals
- Implement the method as follows:
void CFastFoodDlg::EvaluatePrice(void)
{
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
switch(GetCheckedRadioButton(IDC_MEAT, IDC_CHICKEN_BREAST))
{
case IDC_MEAT:
// The beef patty is $1.50
PriceMeat = 1.50;
break;
case IDC_GRILLED_CHICKEN:
case IDC_CHICKEN_BREAST:
// Cheicken breast and grilled chicken are $1.80 each
PriceMeat = 1.80;
break;
default: // Why are we adding this?
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( IsDlgButtonChecked(IDC_CHK_CHEESE) == 1 )
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( IsDlgButtonChecked(IDC_CHK_BACON) == 1 )
PriceBacon = 0.45;
else
PriceBacon = 0.00;
// Now, we can calculte the total price
TotalPrice = PriceBread + PriceMeat + PriceCheese + PriceBacon;
// Convert the price to a null-terminated string
char StrTotalPrice[10];
sprintf(StrTotalPrice, "$%.2f", TotalPrice);
// Display the price
m_TotalPrice.SetWindowText(StrTotalPrice);
}
- To update the price and its display live whenever the user makes a new selection,
generate a BN_CLICKED message for the IDC_MEAT, the
IDC_GRILLED_CHICKEN, the IDC_CHICKEN_BREAST, the
IDC_CHK_CHEESE, and the IDC_CHK_BACON check boxes - In the body of each, simply call the above EvaluatePrice() method. Here are two
examples to save printing paper:
void CFastFoodDlg::OnBnClickedGrilledChicken()