Home

MS Visual C# Applications:
Georgetown Cleaning Services

 

Introduction to Serialization

This application follows the Georgetown Cleaning Services that primarily demonstrated 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 GCS5
  2. In the Solution Explorer, right-click Form1.cs and click Rename
  3. Change the name to OrderProcessing.cs
  4. In the Properties window, change the (Name) of the form to OrderProcessing
  5. Right-click the form and click View Code
  6. Change the name of the form in the Main() method to OrderProcessing
     
    [STAThread]
    static void Main() 
    {
    	Application.Run(new OrderProcessing());
    }
  7. Design the form as follows:
     
    Control Name Text Additional Properties
    GroupBox GroupBox   Order Identification  
    Label Label   Customer Name:  
    TextBox TextBox txtCustomerName    
    Label Label   Customer Phone:  
    TextBox TextBox txtCustomerPhone    
    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 1.25 TextAlign: Right
    TextBox TextBox txtShirtsQuantity 0 TextAlign: Right
    TextBox TextBox txtShirtsSubTotal 0.00 TextAlign: Right
    Label Label   Pants  
    TextBox TextBox txtPantsUnitPrice 1.95 TextAlign: Right
    TextBox TextBox txtPantsQuantity   TextAlign: Right
    TextBox TextBox txtPantsSubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem1 None  
    TextBox TextBox txtItem1UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem1Quantity 0 TextAlign: Right
    TextBox TextBox txtItem1SubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem2 None  
    TextBox TextBox txtItem2UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem2Quantity 0 TextAlign: Right
    TextBox TextBox txtItem2SubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem3 None  
    TextBox TextBox txtItem3UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem3Quantity 0 TextAlign: Right
    TextBox TextBox txtItem3SubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem4 None  
    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 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   Net Price:  
    TextBox TextBox txtNetPrice 0.00 TextAlign: Right
    Button   btnClose Close  
    GroupBox     Maintenance  
    Button   btnSave... Save  
    Button   btnReset Reset  
    Label     Receipt #:  
    TextBox   txtReceiptNumber    
    Button   btnOpen Open...  
  8. Click each combo box. Access its Items property and fill it up as follows:
     
  9. Click OK and save All
  10. On the form, double-click the Time Left control and implement its ValueChanged event as follows:
     
    private void dtpTimeLeft_ValueChanged(object sender, System.EventArgs e)
    {
    	DateTime dateLeft = this.dtpDateLeft.Value;
    	DateTime timeLeft = this.dtpTimeLeft.Value;
    
    	DateTime time9AM   = new 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 = new 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 = new DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1); 
    	this.dtpTimeExpected.Value = new DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1, 8, 0, 0);
    	}
    }
  11. In the Class View, expand GCS5 followed by GCS5, followed by OrderProcessing
  12. Right-click OrderProcessing-> Add -> Add Method...
  13. Set the Method Access to internal
  14. Accept the Return Type as void and set the Method Name to CalculateTotal
  15. Press Enter and implement the method as follows:
     
    internal void CalculateTotal()
    {
    decimal unitPriceShirts = 0.00M, unitPricePants = 0.00M, unitPriceItem1 = 0.00M,
    	unitPriceItem2 = 0.00M, unitPriceItem3 = 0.00M, unitPriceItem4 = 0.00M;
    decimal subTotalShirts = 0.00M, subTotalPants = 0.00M, subTotalItem1 = 0.00M,
    		subTotalItem2 = 0.00M, subTotalItem3 = 0.00M, subTotalItem4 = 0.00M;
    	int    qtyShirts = 1, qtyPants = 1, qtyItem1 = 1,
    		qtyItem2 = 1, qtyItem3 = 1, qtyItem4 = 4;
    decimal cleaningTotal = 0.00M, taxRate = 0.00M, taxAmount = 0.00M, netPrice = 0.00M;
    
    	// Retrieve the unit price of this item
    	// Just in case the user types an invalid value, we are using a try...catch
    	try 
    	{
    		unitPriceShirts = decimal.Parse(this.txtShirtsUnitPrice.Text);
    	}
    	catch(FormatException )
    	{
    MessageBox.Show("The value you entered for the price of shirts is not valid" +
    				"\nPlease try again");
    	}
    
    	// Retrieve the number of this item
    	// Just in case the user types an invalid value, we are using a try...catch
    	try 
    	{
    		qtyShirts  = int.Parse(this.txtShirtsQuantity.Text);
    	}
    	catch(FormatException )
    	{
    MessageBox.Show("The value you entered for the number of shirts is not valid" +
    				"\nPlease try again");
    	}
    	try 
    	{
    		unitPricePants = decimal.Parse(this.txtPantsUnitPrice.Text);
    	}
    	catch(FormatException )
    	{
    	MessageBox.Show("The value you entered for the price of pants is not valid" +
    					"\nPlease try again");
    	}
    
    	try 
    	{
    		qtyPants  = int.Parse(this.txtPantsQuantity.Text);
    	}
    	catch(FormatException )
    	{
    MessageBox.Show("The value you entered for the number of pants is not valid" +
    				"\nPlease try again");
    	}
    
    	try 
    	{
    		unitPriceItem1 = decimal.Parse(this.txtItem1UnitPrice.Text);
    	}
    	catch(FormatException )
    	{
    	MessageBox.Show("The value you entered for the price is not valid" +
    				"\nPlease try again");
    	}
    	try 
    	{
    		qtyItem1  = int.Parse(this.txtItem1Quantity.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("The value you entered is not valid" +
    				"\nPlease try again");
    	}
    
    	try 
    	{
    		unitPriceItem2 = decimal.Parse(this.txtItem2UnitPrice.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("The value you entered for the price is not valid" +
    				"\nPlease try again");
    	}
    	try 
    	{
    		qtyItem2  = int.Parse(this.txtItem2Quantity.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("The value you entered is not valid" +
    				"\nPlease try again");
    	}
    
    	try 
    	{
    		unitPriceItem3 = decimal.Parse(this.txtItem3UnitPrice.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("The value you entered for the price is not valid" +
    				"\nPlease try again");
    	}
    	try 
    	{
    		qtyItem3  = int.Parse(this.txtItem3Quantity.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("The value you entered is not valid" +
    				"\nPlease try again");
    	}
    
    	try 
    	{
    		unitPriceItem4 = decimal.Parse(this.txtItem4UnitPrice.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("The value you entered for the price is not valid" +
    				"\nPlease try again");
    	}
    	try 
    	{
    		qtyItem4  = int.Parse(this.txtItem4Quantity.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("The value you entered is not valid" +
    				"\nPlease try again");
    	}
    
    	// Calculate the sub-total for this item
    	subTotalShirts  = qtyShirts * unitPriceShirts;
    	subTotalPants  = qtyPants * unitPricePants;
    	subTotalItem1  = qtyItem1 * unitPriceItem1;
    	subTotalItem2  = qtyItem2 * unitPriceItem2;
    	subTotalItem3  = qtyItem3 * unitPriceItem3;
    	subTotalItem4  = qtyItem4 * unitPriceItem4;
    
    	// Calculate the total based on sub-totals
    	cleaningTotal = subTotalShirts + subTotalPants + subTotalItem1 +
    			subTotalItem2 + subTotalItem3 + subTotalItem4;
    
    	taxRate = decimal.Parse(this.txtTaxRate.Text);
    	// Calculate the amount owed for the taxes
    	taxAmount = cleaningTotal * taxRate / 100;
    	// Add the tax amount to the total order
    	netPrice  = cleaningTotal + taxAmount;
    
    	// Display the sub-total in the corresponding text box
    	this.txtShirtsSubTotal.Text = subTotalShirts.ToString("F");
    	this.txtPantsSubTotal.Text = subTotalPants.ToString("F");
    	this.txtItem1SubTotal.Text = subTotalItem1.ToString("F");
    	this.txtItem2SubTotal.Text = subTotalItem2.ToString("F");
    	this.txtItem3SubTotal.Text = subTotalItem3.ToString("F");
    	this.txtItem4SubTotal.Text = subTotalItem4.ToString("F");
    
    	this.txtCleaningTotal.Text = cleaningTotal.ToString("F");
    	this.txtTaxAmount.Text = taxAmount.ToString("F");
    	this.txtNetPrice.Text = netPrice.ToString("F");		
    }
  16. Return to the form and click the quantity text box that corresponds to the Shirts
  17. In the Properties window, click the Events button and double-click the Leave field
  18. Implement the event as follows:
     
    private void txtShirtsQuantity_Leave(object sender, System.EventArgs e)
    {
    	CalculateTotal();
    }
  19. Return to the form and click the quantity text box that corresponds to the Pants
  20. In the Events section of the Properties window, double-click the Leave field and implement the event as follows:
     
    private void txtPantsQuantity_Leave(object sender, System.EventArgs e)
    {
    	CalculateTotal();
    }
  21. Return to the form and click the quantity text box that corresponds to the first combo box
  22. In the Events section of the Properties window, double-click the Leave field, and implement the event as follows:
     
    private void txtItem1Quantity_Leave(object sender, System.EventArgs e)
    {
    	if( this.cboItem1.Text != "None" )
    		CalculateTotal();
    	else
    		MessageBox.Show("Make sure you select an item in the combo box");
    }
  23. Return to the form and click the quantity text box that corresponds to the second combo box
  24. In the Events section of the Properties window, double-click the Leave field and implement the event as follows:
     
    private void txtItem2Quantity_Leave(object sender, System.EventArgs e)
    {
    	if( this.cboItem2.Text != "None" )
    		CalculateTotal();
    	else
    		MessageBox.Show("Make sure you select an item in the combo box");
    }
  25. Return to the form and click the quantity text box that corresponds to the third combo box
  26. In the Events section of the Properties window, double-click the Leave field and implement the event as follows:
     
    private void txtItem3Quantity_Leave(object sender, System.EventArgs e)
    {
    	if( this.cboItem3.Text != "None" )
    		CalculateTotal();
    	else
    		MessageBox.Show("Make sure you select an item in the combo box");
    }
  27. Return to the form and click the quantity text box that corresponds to the fourth combo box
  28. In the Events section of the Properties window, double-click the Leave field, and implement the event as follows:
     
    private void txtItem4Quantity_Leave(object sender, System.EventArgs e)
    {
    	if( this.cboItem4.Text != "None" )
    		CalculateTotal();
    	else
    		MessageBox.Show("Make sure you select an item in the combo box");
    }
  29. Return to the form and click the Tax Rate text box
  30. In the Events section of the Properties window, double-click the Leave field and implement the event as follows:
     
    private void txtTaxRate_Leave(object sender, System.EventArgs e)
    {
    	CalculateTotal();
    }
  31. Double-click the Close button and implement its Click event as follows:
     
    private void btnClose_Click(object sender, System.EventArgs e)
    {
    	Close();
    }
  32. Return to the form.
    Double-click the Reset button and implement its event as follows:
     
    private void btnReset_Click(object sender, System.EventArgs e)
    {
    	// Reset the controls
    	this.txtCustomerName.Text = "";
    	this.txtCustomerPhone.Text = "";
    	this.txtShirtsUnitPrice.Text = "1.25";
    	this.txtShirtsQuantity.Text = "0";
    	this.txtShirtsSubTotal.Text = "0.00";
    	this.txtPantsUnitPrice.Text = "1.95";
    	this.txtPantsQuantity.Text = "0";
    	this.txtPantsSubTotal.Text = "0.00";
    	this.cboItem1.Text = "None";
    	this.txtItem1UnitPrice.Text = "0.00";
    	this.txtItem1Quantity.Text = "0";
    	this.txtItem1SubTotal.Text = "0.00";
    	this.cboItem2.Text = "None";
    	this.txtItem2UnitPrice.Text = "0.00";
    	this.txtItem2Quantity.Text = "0";
    	this.txtItem2SubTotal.Text = "0.00";
    	this.cboItem3.Text = "None";
    	this.txtItem3UnitPrice.Text = "0.00";
    	this.txtItem3Quantity.Text = "0";
    	this.txtItem3SubTotal.Text = "0.00";
    	this.cboItem4.Text = "None";
    	this.txtItem4UnitPrice.Text = "0.00";
    	this.txtItem4Quantity.Text = "0";
    	this.txtItem4SubTotal.Text = "0.00";
    	this.txtCleaningTotal.Text = "0.00";
    	this.txtTaxRate.Text = "5.75";
    	this.txtTaxAmount.Text = "0.00";
    	this.txtNetPrice.Text = "0.00";
    	this.txtCustomerName.Focus();
    }
  33. Save all
 

Home Copyright © 2004-2012, FunctionX Next