Visual C++ .NET Examples: Georgetown Cleaning Services

 

Introduction

Some times if you are asked to create an application for a store that sells a specific number of items based on a fixed list, you may ask for the list of items so you can create its elements in the application. Here is an example:

For such an application, when the list of items changes, you may have to modify the application, recompile, and redistribute (or re-install it). One solution to this problem would consist of providing placeholders for an employee to fill out and process the order with them. Another problem that can be common with this type of application is that, while the form may be crowded with too many objects, most of the time, some items are rarely ordered.

In this exercise, to make this type of application a little more flexible, we will use a few combo boxes that allow the user to simply select the items that are valid for the order.

Practical Learning Practical Learning: Introducing Buttons

  1. Start a new Windows Forms Application named GCS2
  2. Set the form's icon to Drive:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\icons\Misc\BULLSEYE.ICO
  3. On the main menu, click Project -> Add Resource...
  4. In the Add Resource dialog box, double-click Icon
  5. In the Class View, click IDI_ICON1
  6. In the Properties window, change the filename to Calculate.ico and change the ID to IDI_CALCULATE
  7. Right-click a white area in the drawing section and click New Image Type...
  8. In the New Icon Image Type dialog box, double-click 16x16, 16 Colors
  9. Design it as follows:
     
    Calculator - Icon Design
  10. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 32x32, 16 Colors
  11. Right-click a white area in the drawing section and click Delete Image Type
  12. Save the icon
  13. On the main menu, click Project -> Add Resource...
  14. In the Add Resource dialog box, click Icon and click New
  15. In the Class View, click IDI_ICON2
  16. In the Properties window, change the filename to SubTotal.ico and change the ID to IDI_SUBTOTAL
  17. Right-click a white area in the drawing section and click New Image Type...
  18. In the New Icon Image Type dialog box, double-16x16, 16 Colors
  19. Design the icon as follows:
     
    SubTotal - Icon Design
  20. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 32x32, 16 Colors
  21. Right-click a white area in the drawing section and click Delete Image Type
  22. Save the icon
  23. Return to the form (Form1.h [Design]) and design it as follows:
     
    Control Name Text Additional Properties
    GroupBox GroupBox   Order Identification  
    Label Label   Customer Name:  
    TextBox TextBox txtCustomerName    
    Label Label   Date Left:  
    DateTimePicker DateTimePicker dtpDateLeft   Format: Custom
    Custom Format: dddd dd MMM yyyy
    Label Label   Time Left:  
    DateTimePicker DateTimePicker dtpTimeLeft   Format: Time
    Label Label   Date Expected:  
    DateTimePicker DateTimePicker dtpDateExpected   Format: Custom
    Custom Format: dddd dd MMM yyyy
    Label     Time Expected:  
    DateTimePicker DateTimePicker dtpTimeExpected   Format: Time
    GroupBox GroupBox   Order Processing  
    Label Label   Item Type  
    Label Label   Unit Price  
    Label Label   Qty  
    Label Label   Sub Total  
    Label Label   Shirts  
    TextBox TextBox txtShirtsUnitPrice 0.95 TextAlign: Right
    TextBox TextBox txtShirtsQuantity 0 TextAlign: Right
    Button Button btnCalcShirts   Image: SubTotal.ico
    TextBox TextBox txtShirtsSubTotal 0.00 TextAlign: Right
    Label Label   Pants  
    TextBox TextBox txtPantsUnitPrice 2.75 TextAlign: Right
    TextBox TextBox txtPantsQuantity   TextAlign: Right
    Button Button btnCalcPants   Image: SubTotal.ico
    TextBox TextBox txtPantsSubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem1 None  
    TextBox TextBox txtItem1UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem1Quantity 0 TextAlign: Right
    Button Button btnCalcItem1   Image: SubTotal.ico
    TextBox TextBox txtItem1SubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem2 None  
    TextBox TextBox txtItem2UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem2Quantity 0 TextAlign: Right
    Button Button btnCalcItem2   Image: SubTotal.ico
    TextBox TextBox txtItem2SubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem3 None  
    TextBox TextBox txtItem3UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem3Quantity 0 TextAlign: Right
    Button Button btnCalcItem3   Image: SubTotal.ico
    TextBox TextBox txtItem3SubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem4 None  
    TextBox TextBox txtItem4UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem4Quantity 0 TextAlign: Right
    Button Button btnCalcItem4   Image: SubTotal.ico
    TextBox TextBox txtItem4UnitPrice 0.00 TextAlign: Right
    Button Button btnCalculate Calculate  
    GroupBox GroupBox   Order Summary  
    Label Label   Order Total:  
    TextBox TextBox txtOrderTotal 0.00 TextAlign: Right
    Label Label   TaxRate  
    TextBox TextBox txtTaxRate 5.75 TextAlign: Right
    Label Label   %  
    Label Label   Tax Amount  
    TextBox TextBox txtTaxAmount 0.00 TextAlign: Right
    Label Label   Order Price  
    TextBox TextBox txtOrderPrice 0.00 TextAlign: Right
  24. Click each combo box. Access its Items property and fill it up as follows:
     
  25. Click OK and save All

 

Application Implementation

As a dry cleaning applications, there are some behaviors we will apply. About dates, if a customer leaves his or her clothes in the morning before 9AM, we will promise that the clothes can be ready on the same day after 5PM. If a customer leaves the clothes after 9AM, they will be ready only the following day.

To process an order, we provided the clerk with 4 combo boxes to select some of the most regularly ordered cleaning items. The combo boxes also allow the user to type an item that is not in the list. Also, because we can't or we won't predict the prices of these items, we let the user specify their price.

Once an order is ready, we will calculate it using classic operations.

Practical Learning Practical Learning: Implementing the Application

  1. On the form, double-click the Time Left control and implement its ValueChanged event as follows:
     
    System::Void dtpTimeLeft_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 DateTime dateLeft = this->dtpDateLeft->Value;
    	 DateTime timeLeft = this->dtpTimeLeft->Value;
    
    	 DateTime time9AM   = DateTime(timeLeft.Year, timeLeft.Month, timeLeft.Day, 9, 0, 0);
    
    	 // If the customer leaves clothes before 9AM...
    	 if( timeLeft <= time9AM )
    	 {
    		 // ... then they should be ready the same day after 5PM
    		 this->dtpDateExpected->Value = dateLeft;
    		 this->dtpTimeExpected->Value = DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day, 17, 0, 0);
    	 }
    	 else
    	 {
    		 // If the clothese were left after 9AM, they will be availablethe following morning at 8AM
    		 this->dtpDateExpected->Value = DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1); 
    		 this->dtpTimeExpected->Value = DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1, 8, 0, 0);
    	 }
    }
  2. Return to the form. Double-click the button on the right side of the Shirts text box and implement its Click event as follows:
     
    System::Void btnCalcShirts_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 int quantity = 1;
    	 double unitPrice, subTotal;
    
    	 // Retrieve the number of this item
    	 // Just in case the user types an invalid value, we are using a try...catch
    	 try {
    		 quantity  = this->txtShirtsQuantity->Text->ToInt16(0);
    	 }
    	 catch(FormatException *)
    	 {
    		 MessageBox::Show(S"The value you entered for the number of shirts is not valid"
    			              S"\nPlease try again");
    	 }
    
    	 // Retrieve the unit price of this item
    	 // Just in case the user types an invalid value, we are using a try...catch
    	 try {
    		 unitPrice = this->txtShirtsUnitPrice->Text->ToDouble(0);
    	 }
    	 catch(FormatException *)
    	 {
    		 MessageBox::Show(S"The value you entered for the price of shirts is not valid"
    			              S"\nPlease try again");
    	 }
    
    	 // Calculate the sub-total for this item
    	 subTotal  = quantity * unitPrice;
    
    	 // Display the sub-total in the corresponding text box
    	 this->txtShirtsSubTotal->Text = subTotal.ToString(S"F");
    }
  3. Return to the form. Double-click the button on the right side of the Pants text bix and implement its Click event as follows:
     
    System::Void btnCalcPants_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 int quantity = 1;
    	double unitPrice, subTotal;
    
    	try {
    		quantity  = this->txtPantsQuantity->Text->ToInt16(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The value you entered for the number of pants is not valid"
    			             S"\nPlease try again");
    	}
    
    	try {
    		unitPrice = this->txtPantsUnitPrice->Text->ToDouble(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The value you entered for the price of pants is not valid"
    			             S"\nPlease try again");
    	}
    
    	subTotal  = quantity * unitPrice;
    
    	this->txtPantsSubTotal->Text = subTotal.ToString("F");
    }
  4. Return to the form. Double-click the button for the first combo box
  5. Return to the form. Double-click the button for the second combo box
  6. Return to the form. Double-click the button for the third combo box
  7. Return to the form. Double-click the button for the fourth combo box
  8. Implement their Click events as follows:
     
    System::Void btnCalcItem1_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	int quantity = 1;
    	double unitPrice, subTotal;
    
    	try {
    		quantity  = this->txtItem1Quantity->Text->ToInt16(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The value you entered is not valid"
    			             S"\nPlease try again");
    	}
    
    	try {
    		unitPrice = this->txtItem1UnitPrice->Text->ToDouble(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The value you entered for the price is not valid"
    			             S"\nPlease try again");
    	}
    
    	subTotal  = quantity * unitPrice;
    
    	this->txtItem1SubTotal->Text = subTotal.ToString("F");
    }
    
    private: System::Void btnCalcItem2_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	int quantity = 1;
    	double unitPrice, subTotal;
    
    	try {
    		quantity  = this->txtItem2Quantity->Text->ToInt16(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The value you entered is not valid"
    			             S"\nPlease try again");
    	}
    
    	try {
    		unitPrice = this->txtItem2UnitPrice->Text->ToDouble(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The value you entered for the price is not valid"
    			             S"\nPlease try again");
    	}
    
    	subTotal  = quantity * unitPrice;
    
    	this->txtItem2SubTotal->Text = subTotal.ToString("F");
    }
    
    private: System::Void btnCalcItem3_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	int quantity = 1;
    	double unitPrice, subTotal;
    
    	try {
    		quantity  = this->txtItem3Quantity->Text->ToInt16(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The value you entered is not valid"
    			             S"\nPlease try again");
    	}
    
    	try {
    		unitPrice = this->txtItem3UnitPrice->Text->ToDouble(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The value you entered for the price is not valid"
    			             S"\nPlease try again");
    	}
    
    	subTotal  = quantity * unitPrice;
    
    	this->txtItem3SubTotal->Text = subTotal.ToString("F");
    }
    
    private: System::Void btnCalcItem4_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	int quantity = 1;
    	double unitPrice, subTotal;
    
    	try {
    		quantity  = this->txtItem4Quantity->Text->ToInt16(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The value you entered is not valid"
    			             S"\nPlease try again");
    	}
    
    	try {
    		unitPrice = this->txtItem4UnitPrice->Text->ToDouble(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The value you entered for the price is not valid"
    			             S"\nPlease try again");
    	}
    
    	subTotal  = quantity * unitPrice;
    
    	this->txtItem4SubTotal->Text = subTotal.ToString("F");
    }
  9. Return to the form. Double-click the Calculate button and implement its Click event as follows:
     
    System::Void btnCalculate_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	double priceShirts, pricePants,
    		   priceItem1, priceItem2, priceItem3, priceItem4,
    		   totalOrder;
    	double taxRate, taxAmount;
    	double netPrice;
    
    	// Just in case the user forgot to calculate the sub totals, do it now
    	this->btnCalcShirts_Click(sender, e);
    	this->btnCalcPants_Click(sender, e);
    	this->btnCalcItem1_Click(sender, e);
    	this->btnCalcItem2_Click(sender, e);
    	this->btnCalcItem3_Click(sender, e);
    	this->btnCalcItem4_Click(sender, e);
    
    	// Retrieve the value of the sub-total for each category of items
    	priceShirts  = this->txtShirtsSubTotal->Text->ToDouble(0);
    	pricePants   = this->txtPantsSubTotal->Text->ToDouble(0);
    	priceItem1 = this->txtItem1SubTotal->Text->ToDouble(0);
    	priceItem2 = this->txtItem2SubTotal->Text->ToDouble(0);
    	priceItem3 = this->txtItem3SubTotal->Text->ToDouble(0);
    	priceItem4 = this->txtItem4SubTotal->Text->ToDouble(0);
    
    	// Calculate the total
    	totalOrder = priceShirts + pricePants + 
    		         priceItem1 + priceItem2 + priceItem3 + priceItem4;
    
    	// Retrieve the value of the tax rate
    	try {
    		taxRate = this->txtTaxRate->Text->ToDouble(0);
    	}
    	catch(FormatException *)
    	{
    		MessageBox::Show(S"The tax rate you entered is invalid"
    			             S"\nPlease try again");
    	}
    
    	// Calculate the amount owed for the taxes
    	taxAmount = totalOrder * taxRate / 100;
    	// Add the tax amount to the total order
    	netPrice  = totalOrder + taxAmount;
    			
    	// Display the values of the order summary
    	this->txtOrderTotal->Text = totalOrder.ToString("C");
    	this->txtTaxAmount->Text  = taxAmount.ToString("C");
    	this->txtOrderPrice->Text   = netPrice.ToString("C");
    }
  10. Execute the application to test the form
  11. Close the form and return to your programming environment
 

Home Copyright © 2004-2010 FunctionX, Inc.