Home

Visual C# Examples:
Georgetown Cleaning Services

 

Command Buttons

This exercise is meant to apply the concepts of a Button control. It is used to process simple orders for a cleaning store.

 

Practical Learning Practical Learning: Introducing Buttons

  1. Start a new Windows Application named GCS1
  2. Set the form's icon to Drive:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\icons\Misc\BULLSEYE.ICO
  3. Design the form as follows:
     
    Georgetown Cleaning Services
    Control Name Text Additional Properties
    GroupBox GroupBox   Order Setup  
    Label Label   Customer Name:  
    TextBox TextBox txtCustName    
    Label Label   Order Date:  
    MaskedEdBox Masked Edit Control txtOrderDate   Format: dddd dd mmmm yyyy
    Mask: ##-##-##
    Label Label   Order Time:  
    MaskedEdBox Masked Edit Control txtOrderTime   Format: hh:mm AM/PM
    Mask: ##:##
    GroupBox GroupBox   Order Processing  
    Label Label   Item Type  
    Label Label   Qty  
    Label Label   Unit Price  
    Label Label   Sub Total  
    Label Label   Shirts  
    TextBox TextBox txtQtyShirts 0 TextAlign: Right
    TextBox TextBox txtPriceShirts 0.95 TextAlign: Right
    TextBox TextBox txtTotalShirts 0.00 TextAlign: Right
    ReadOnly: True
    Label Label   Pants  
    TextBox TextBox txtQtyPants 0 TextAlign: Right
    TextBox TextBox txtPricePants 2.95 TextAlign: Right
    TextBox TextBox txtTotalPants 0.00 TextAlign: Right
    ReadOnly: True
    Label Label   Dresses  
    TextBox TextBox txtQtyDresses 0 TextAlign: Right
    TextBox TextBox txtPriceDresses 2.95 TextAlign: Right
    TextBox TextBox txtTotaldresses 4.55 TextAlign: Right
    ReadOnly: True
    Button Button btnCalculate Calculate  
    GroupBox GroupBox   Order Summary  
    Label Label   Total Order  
    TextBox TextBox txtTotalOrder 0.00 TextAlign: Right
    ReadOnly: True
    Label Label   TaxRate  
    TextBox TextBox txtTaxRate 5.75 TextAlign: Right
    Label Label   %  
    Label Label   Tax Amount  
    TextBox TextBox txtTaxAmount 0.00 TextAlign: Right
    ReadOnly: True
    Label Label   Net Price  
    TextBox TextBox txtNetPrice 0.00 TextAlign: Right
    ReadOnly: True
    Button Button btnClose Close  
  4. Save All
  5. On the main menu, click File -> New -> File...
  6. In the New File dialog box, double-click Icon File
  7. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 16x16, 16 Colors
  8. Save it as Calculate in the main folder of the current project
  9. Change its design as follows:
     
  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 New Item...
  14. In the Add New Item dialog box, click Icon File
  15. Change the name to SubTotal and click Open
  16. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 16x16, 16 Colors
  17. Change its design as follows:
     
  18. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 32x32, 16 Colors
  19. Right-click a white area in the drawing section and click Delete Image Type
  20. Save the icon
  21. Add three buttons inside the lower-left group box and delete the content of their Text property
  22. Complete the design of the form as follows:
     
    Georgetown Cleaning Services
    Button Name Image ImageAlign TextAlign
    btnShirts SubTotal    
    btnPants SubTotal    
    btnDresses SubTotal    
    btnCalculate Calculate TopCenter BottomCenter
  23. On the main menu, click View Tab Order and click the controls in the following order (observe the sequence in the Order Processing group box)
     
    Georgetown Cleaning Services - Tab Order
  24. Save all
  25. Double-click the button for the shirts and implement its Click event as follows:
     
    private void btnShirts_Click(object sender, System.EventArgs e)
    {
    	int quantity = 1;
    	double unitPrice = 0.00, subTotal;
    
    	// Retrieve the number of this item
    	// Just in case the user types an invalid value, we are using a try...catch
    	try 
    	{
    		quantity  = int.Parse(this->txtQtyShirts->Text);
    	}
    	catch(FormatException)
    	{
    		MessageBox::Show("The value you entered for the number of shirts is not valid" +
    			"\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 = double.Parse(this->txtPriceShirts->Text);
    	}
    	catch(FormatException)
    	{
    		MessageBox::Show("The value you entered for the price of shirts is not valid" +
    			"\nPlease try again");
    	}
    
    	// Calculate the sub-total for this item
    	subTotal  = quantity * unitPrice;
    
    	// Display the sub-total in the corresponding text box
    	this->txtTotalShirts->Text = subTotal.ToString("F");
    }
  26. Double-click the button for the pants and implement its Click event as follows:
     
    private void btnPants_Click(object sender, System.EventArgs e)
    {
    	int quantity = 1;
    	double unitPrice = 0.00, subTotal;
    
    	try 
    	{
    		quantity  = int.Parse(this->txtQtyPants->Text);
    	}
    	catch(FormatException)
    	{
    		MessageBox::Show("The value you entered for the number of pants is not valid" +
    			"\nPlease try again");
    	}
    
    	try 
    	{
    		unitPrice = double.Parse(this->txtPricePants->Text);
    	}
    	catch(FormatException)
    	{
    		MessageBox::Show("The value you entered for the price of pants is not valid" +
    			"\nPlease try again");
    	}
    
    	subTotal  = quantity * unitPrice;
    
    	this->txtTotalPants->Text = subTotal.ToString("F");
    }
  27. Double-click the button for the dresses and implement its Click event as follows:
     
    private void btnDresses_Click(object sender, System.EventArgs e)
    {
    	int quantity = 1;
    	double unitPrice = 0.00, subTotal;
    
    	try 
    	{
    		quantity  = int.Parse(this->txtQtyDresses->Text);
    	}
    	catch(FormatException)
    	{
    		MessageBox::Show("The value you entered for the number of dresses is not valid" +
    			"\nPlease try again");
    	}
    
    	try 
    	{
    		unitPrice = double.Parse(this->txtPriceDresses->Text);
    	}
    	catch(FormatException)
    	{
    		MessageBox::Show("The value you entered for the price of dresses is not valid" +
    			"\nPlease try again");
    	}
    
    	subTotal  = quantity * unitPrice;
    
    	this->txtTotalDresses->Text = subTotal.ToString("F");
    }
  28. Double-click the Calculate button and implement its Click event as follows:
     
    private void btnCalculate_Click(object sender, System.EventArgs e)
    {
    	double priceShirts, pricePants, priceDresses, totalOrder;
    	double taxRate = 0.00, taxAmount;
    	double netPrice;
    
    	// Retrieve the value of the sub-total for each category of items
    	priceShirts  = double.Parse(this->txtTotalShirts->Text);
    	pricePants   = double.Parse(this->txtTotalPants->Text);
    	priceDresses = double.Parse(this->txtTotalDresses->Text);
    
    	// Calculate the total
    	totalOrder = priceShirts + pricePants + priceDresses;
    
    	// Retrieve the value of the tax rate
    	try 
    	{
    		taxRate = double.Parse(this->txtTaxRate->Text);
    	}
    	catch(FormatException)
    	{
    		MessageBox::Show("The tax rate you entered is invalid" +
    			"\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->txtTotalOrder->Text = totalOrder.ToString("C");
    	this->txtTaxAmount->Text  = taxAmount.ToString("C");
    	this->txtNetPrice->Text   = netPrice.ToString("C");
    }
  29. Double-click the Close button and implement its Click event as follows:
     
    private void btnClose_Click(object sender, System.EventArgs e)
    {
    	Close();
    }
  30. Execute the application to test the form. In the Order Date text box, type 081103 and in the Order Time, type 0728 then complete the rest of the order
     
    Georgetown Cleaning Services - Order Processing
  31. Close the form and return to your programming environment
 

Home Copyright © 2004-2010 FunctionX, Inc.