Home

MS Visual C++ .NET Applications: Georgetown Cleaning Services

 

Introduction to Serialization

This application follows the Georgetown Cleaning Services that was primarily used to demonstrate the use of various Windows controls such as the date picker, the time picker, and bitmap buttons. One of the issues that was not dealt with was the ability to save the customers orders.

In this application, after a customer's order has been processed, we will allow the user to save it.

Practical Learning Practical Learning: Introducing File-Based Databases

  1. Start a new Windows Forms Application named GCS3
  2. To add a new form, on the main menu, click Project -> Add New Item...
  3. In the Templates list of the Add New Item dialog box, click Windows Forms (.NET)
  4. Set the Name to NewCleaningOrder and press Enter
  5. Set the form's icon to Drive:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\icons\Writing\NOTE16.ICO
  6. On the main menu, click Project -> Add Resource...
  7. In the Add Resource dialog box, double-click Icon
  8. In the Class View, click IDI_ICON1
  9. In the Properties window, change the filename to Calculate.ico and change the ID to IDI_CALCULATE
  10. Right-click a white area in the drawing section and click New Image Type...
  11. In the New Icon Image Type dialog box, double-click 16x16, 16 Colors
  12. Design it as follows:
     
    Calculator - Icon Design
  13. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 32x32, 16 Colors
  14. Right-click a white area in the drawing section and click Delete Image Type
  15. Save the icon
  16. On the main menu, click Project -> Add Resource...
  17. In the Add Resource dialog box, click Icon and click New
  18. In the Class View, click IDI_ICON2
  19. In the Properties window, change the filename to SubTotal.ico and change the ID to IDI_SUBTOTAL
  20. Right-click a white area in the drawing section and click New Image Type...
  21. In the New Icon Image Type dialog box, double-16x16, 16 Colors
  22. Design the icon as follows:
     
    SubTotal - Icon Design
  23. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 32x32, 16 Colors
  24. Right-click a white area in the drawing section and click Delete Image Type
  25. Save the icon
  26. 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 txtItem4SubTotal 0.00 TextAlign: Right
    Button Button btnCalculate Calculate  
    GroupBox GroupBox   Order Summary  
    Label Label   Cleaning Total:  
    TextBox TextBox txtOrderTotal 0.00 TextAlign: Right
    Label Label   Tax Rate:  
    TextBox TextBox txtTaxRate 5.75 TextAlign: Right
    Label Label   %  
    Label Label   Tax Amount:  
    TextBox TextBox txtTaxAmount 0.00 TextAlign: Right
    Label Label   Order Total:  
    TextBox TextBox txtOrderPrice 0.00 TextAlign: Right
    GroupBox     Maintenance  
    Label     Save this order in  
    DateTimePicker   dtpDateProcessed   Format: Custom
    CustomFormat: dd MMM yyyy
    Button   btnSave Save  
    Button   btnNewOrder Start New Cleaning Order  
    Button   btnClose Close  
  27. Click each combo box. Access its Items property and fill it up as follows:
     
  28. Click OK and save All
  29. On the form, double-click the Time Left control and implement its ValueChanged event as follows:
     
    private: 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);
    	 }
    }
  30. 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");
    }
  31. Return to the form. Double-click the button on the right side of the Pants text box 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");
    }
  32. Return to the form. Double-click the button for the first combo box
  33. Return to the form. Double-click the button for the second combo box
  34. Return to the form. Double-click the button for the third combo box
  35. Return to the form. Double-click the button for the fourth combo box
  36. 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");
    }
  37. Return to the form. Double-click the Calculate button and implement its Click event as follows:
     
    private: System::Void btnCalculate_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	double priceShirts, pricePants,
    		   priceItem1, priceItem2, priceItem3, priceItem4,
    		   cleaningTotal;
    	double taxRate, taxAmount;
    	double orderTotal;
    
    	// 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
    	cleaningTotal = 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 = cleaningTotal * taxRate / 100;
    	// Add the tax amount to the total order
    	orderTotal  = cleaningTotal + taxAmount;
    			
    	// Display the values of the order summary
    	this->txtCleaningTotal->Text = cleaningTotal.ToString(S"F");
    	this->txtTaxAmount->Text  = taxAmount.ToString(S"F");
    	this->txtOrderTotal->Text   = orderTotal.ToString(S"F");
    }
  38. Return to the New Cleaning Order form and double the Start New Cleaning Order
  39. Implement the event as follows:
     
    private: System::Void btnNewOrder_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 this->txtCustomerName->Text = S"";
    	 this->txtCustomerPhone->Text = S"";
    
    	 this->txtShirtsUnitPrice->Text = S"0.95";
    	 this->txtShirtsQuantity->Text  = S"0";
    	 this->txtShirtsSubTotal->Text = S"0.00";
    
    	 this->txtPantsUnitPrice->Text = S"2.75";
    	 this->txtPantsQuantity->Text = S"0";
    	 this->txtPantsSubTotal->Text = S"0.00";
    
    	 this->txtItem1UnitPrice->Text = S"0.00";
    	 this->txtItem1Quantity->Text = S"0";
    	 this->txtItem1SubTotal->Text = S"0.00";
    
    	 this->txtItem2UnitPrice->Text = S"0.00";
    	 this->txtItem2Quantity->Text = S"0";
    	 this->txtItem2SubTotal->Text = S"0.00";
    
    	 this->txtItem3UnitPrice->Text = S"0.00";
    	 this->txtItem3Quantity->Text = S"0";
    	 this->txtItem3SubTotal->Text = S"0.00";
    
    	 this->txtItem4UnitPrice->Text = S"0.00";
    	 this->txtItem4Quantity->Text = S"0";
    	 this->txtItem4SubTotal->Text = S"0.00";
    
    	 this->txtCleaningTotal->Text = S"0.00";
    	 this->txtTaxRate->Text = S"5.75";
    	 this->txtTaxAmount->Text = S"0.00";
    	 this->txtOrderTotal->Text = S"0.00";
    
    	 this->txtCustomerName->Focus();
    }
  40. Return to the form
  41. Double-click the Close button and implement its Click event as follows:
     
    System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
  42. To add a new form, on the main menu, click Project -> Add New Item...
  43. In the Templates list of the Add New Item dialog box, click Windows Forms (.NET)
  44. Set the Name to CleaningOrders and press Enter
  45. Design the form as follows:
     
    Control Name Text Additional Properties
    Label Label   Open the order(s) processed on  
    DateTimePicker DateTimePicker dtpDateProcessed    
    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 Picked Up:  
    DateTimePicker DateTimePicker dtpDatePickedUp   Format: Custom
    Custom Format: dddd dd MMM yyyy
    Label     Time Picked UP:  
    DateTimePicker DateTimePicker dtpTimePickedUp   Format: Time
    GroupBox GroupBox   Items Processed  
    Label Label   Item Type  
    Label Label   Unit Price  
    Label Label   Qty  
    Label Label   Sub Total  
    Label Label   Shirts  
    TextBox TextBox txtShirtsUnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtShirtsQuantity 0 TextAlign: Right
    TextBox TextBox txtShirtsSubTotal 0.00 TextAlign: Right
    Label Label   Pants  
    TextBox TextBox txtPantsUnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtPantsQuantity   TextAlign: Right
    TextBox TextBox txtPantsSubTotal 0.00 TextAlign: Right
    TextBox TextBox txtItem1Name    
    TextBox TextBox txtItem1UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem1Quantity 0 TextAlign: Right
    TextBox TextBox txtItem1SubTotal 0.00 TextAlign: Right
    TextBox TextBox txtItem2Name    
    TextBox TextBox txtItem2UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem2Quantity 0 TextAlign: Right
    TextBox TextBox txtItem2SubTotal 0.00 TextAlign: Right
    TextBox TextBox txtItem3Name    
    TextBox TextBox txtItem3UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem3Quantity 0 TextAlign: Right
    TextBox TextBox txtItem3SubTotal 0.00 TextAlign: Right
    TextBox TextBox txtItem4Name    
    TextBox TextBox txtItem4UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem4Quantity 0 TextAlign: Right
    TextBox TextBox txtItem4SubTotal 0.00 TextAlign: Right
    GroupBox GroupBox   Order Summary  
    Label Label   Cleaning Total:  
    TextBox TextBox txtCleaningTotal 0.00 TextAlign: Right
    Label Label   Tax Rate:  
    TextBox TextBox txtTaxRate 0.00 TextAlign: Right
    Label Label   %  
    Label Label   Tax Amount:  
    TextBox TextBox txtTaxAmount 0.00 TextAlign: Right
    Label Label   Total Order:  
    TextBox TextBox txtTotalOrder 0.00 TextAlign: Right
    Button   btnClose Close  
    GroupBox     Navigation  
    Button   btnFirst First  
    Button   btnPrevious Previous  
    Button   btnNext Next  
    Button   btnLast Last  
  46. Double-click the Close button and implement its Click event as follows:
     
    System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
  47. Display the first form (Form1.h [Design]) and design it as follows:
     
    Button Name Button Text
    btnNewOrder New Cleaning Order
    btnOrders Open Existing Orders
    btnClose Close
  48. Double-click the New Cleaning Order button
  49. Return to the form and double-click the Open Existing Orders button
  50. Return to the form and double-click the Close button
  51. Implement the Click events as follows:
     
    #pragma once
    
    #include "NewCleaningOrder.h"
    #include "CleaningOrders.h"
    
    namespace GCS3
    {
    	. . . No Change
    		
    	private: System::Windows::Forms::Button *  btnNewOrder;
    	private: System::Windows::Forms::Button *  btnOrders;
    	private: System::Windows::Forms::Button *  btnClose;
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			
    			. . . No Change
    
    		}	
    private: System::Void btnNewOrder_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			NewCleaningOrder *frmOrder = new NewCleaningOrder();
    			 frmOrder->ShowDialog();
    		 }
    
    private: System::Void btnOrders_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 CleaningOrders *frmOrder = new CleaningOrders();
    			 frmOrder->ShowDialog();
    		 }
    
    private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 Close();
    		 }
    
    	};
    }
  52. Save all
 

Home Copyright © 2004-2005 FunctionX, Inc. Next