Home

File-Based Applications:
Bethesda Car Rental

 

Customers

The essence of any business is to present or sell products to a customers. These are people or entities that buy what ever is on sale. In our application, we will create simple information about each customer. This information will be stored in a text file and be able to open it when needed. To make our assignment easy, we will use the same approach and steps we used for the employees.

 

Practical Learning Practical Learning: Creating Employees

  1. To add a new form to the application, on the main menu, click Project -> Add Windows Forms
  2. Set the Name to Customers and press Enter
  3. From the Toolbox, add a ListView to the form
  4. While the new list view is still selected, in the Properties window, click the ellipsis button of the Columns field and create the columns as follows:
     
    (Name) Text Width
    colDrvLicNbr Driver's Lic. # 100
    colFullName Full Name 100
    colAddress Address 160
    colCity City 100
    colState State 38
    colZIPCode ZIPCode  
    colCountry Country  
  5. Design the form as follows: 
     
     
    Control Text Name Other Properties
    ListView   lvwEmployees View: Details
    GridLines: True
    FullRowSelect: True
    Button New Customer btnNewCustomer  
    Button Close btnClose  
  6. To add another form to the application, on the main menu, click Project -> Add Windows Forms
  7. Set the Name to NewCustomer and press Enter
  8. Design the form as follows: 
     
     
    Control Text Name Properties
    Label Driver's Lic. #:    
    TextBox   txtDrvLicNbr Modifiers: Public
    Label Full Name:    
    TextBox   txtFullName Modifiers: Public
    Label Address:    
    TextBox   txtAddress Modifiers: Public
    Label City:    
    TextBox   txtCity Modifiers: Public
    Label State:    
    TextBox MD txtState Modifiers: Public
    Label ZIP Code:    
    TextBox   txtZIPCode Modifiers: Public
    Label Country:    
    TextBox USA txtCountry Modifiers: Public
    Button New Customer btnNewCustomer DialogResult: OK
    Button Close btnClose DialogResult: Cancel
    Form     AcceptButton: btnNewCustomer
    CancelButton: btnClose
    FormBorderStyle: FixedDialog
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskbar: False
  9. To add a new class to the project, on the main menu, click Project -> Add Class...
  10. Set the Class Name to Customer and click Finish
  11. Change the Customer.cs file as follows:
     
    using System;
    
    namespace BCR2
    {
    	/// <summary>
    	/// Summary description for Customer.
    	/// </summary>
    	[Serializable]
    	public class Customer
    	{
    		public string DrvLicNbr;
    		public string FullName;
    		public string Address;
    		public string City;
    		public string State;
    		public string ZIPCode;
    		public string Country;
    
    		public Customer()
    		{
    			DrvLicNbr = "A-000-000-000-000";
    			FullName  = "John Doe";
    			Address   = "123 Main Street";
    			City      = "Good City";
    			State     = "MD";
    			ZIPCode   = "20900";
    			Country   = "USA";
    		}
    		
    		public Customer(string DrvLic, string fName, string adr,
    			string ct, string ste, string zip, string cty)
    		{
    			DrvLicNbr = DrvLic;
    			FullName  = fName;
    			Address   = adr;
    			City      = ct;
    			State     = ste;
    			ZIPCode   = zip;
    			Country   = cty;
    		}
    	}
    }
  12. In the Class View, expand BCR2 and BCR2
  13. Right-click Employees -> Add -> Add Method...
  14. Set the Method Access to internal
  15. Set the Return Type to void
  16. Set the Method Name to ShowCustomers and its Access to private
  17. Click Finish 
  18. Return to the Customers form and double-click an empty area of its body to generate its Load event
  19. Return to the Customers form and double-click its New Customer button
  20. Return to the Customers form and double-click its Close button
  21. Change the file as follows:
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Soap;
    
    namespace BCR2
    {
    	/// <summary>
    	/// Summary description for Customers.
    	/// </summary>
    	public class Customers : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.ColumnHeader colZIPCode;
    		private System.Windows.Forms.ColumnHeader colState;
    		private System.Windows.Forms.ColumnHeader colCountry;
    		private System.Windows.Forms.Button btnClose;
    		private System.Windows.Forms.Button btnNewCustomer;
    		private System.Windows.Forms.ColumnHeader colDrvLicNbr;
    		private System.Windows.Forms.ListView lvwCustomers;
    		private System.Windows.Forms.ColumnHeader colFullName;
    		private System.Windows.Forms.ColumnHeader colAddress;
    		private System.Windows.Forms.ColumnHeader colCity;
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    		ArrayList lstCustomers;
    
    		. . . No Change
    
    internal void ShowCustomers()
    {
    	SoapFormatter bcrSoap = new SoapFormatter();
    	string strFilename = "Customers.bcr";
    			 
    	if( File.Exists(strFilename) )
    	{
    		FileStream bcrStream = new FileStream(strFilename, FileMode.Open, 
    			FileAccess.Read, FileShare.Read);
    		ArrayList lstCust = (ArrayList)bcrSoap.Deserialize(bcrStream);
    		bcrStream.Close();
    
    		Customer cust;
    
    		lvwCustomers.Items.Clear();
    
    		for(int i = 0; i < lstCust.Count; i++)
    		{
    			cust = (Customer)lstCust[i];
    		ListViewItem lviCustomer = new ListViewItem(cust.DrvLicNbr);
    		lviCustomer.Font = new Font("Georgia", 8, FontStyle.Bold);
     
    			if( i % 2 == 0 )
    			{
    				lviCustomer.BackColor = Color.Blue;
    				lviCustomer.ForeColor = Color.White;
    			}
    			else
    			{
    				lviCustomer.BackColor = Color.LightBlue;
    				lviCustomer.ForeColor = Color.Blue;
    			}
    
    			lviCustomer.SubItems.Add(cust.FullName);
    			lviCustomer.SubItems.Add(cust.Address);
    			lviCustomer.SubItems.Add(cust.City);
    			lviCustomer.SubItems.Add(cust.State);
    			lviCustomer.SubItems.Add(cust.ZIPCode);
    			lviCustomer.SubItems.Add(cust.Country);
    			lvwCustomers.Items.Add(lviCustomer);
    		}
    	}
    }
    
    private void Customers_Load(object sender, System.EventArgs e)
    {
    	 lstCustomers = new ArrayList();
    	 SoapFormatter bcrSoap = new SoapFormatter();
    	 string strFilename = "Customers.bcr";
    			 
    	 if( File.Exists(strFilename) )
    	 {
    		FileStream bcrStream = new FileStream(strFilename, 
    			FileMode.Open, FileAccess.Read, FileShare.Read);
    		 lstCustomers = (ArrayList)bcrSoap.Deserialize(bcrStream);
    
    		 bcrStream.Close();
    	 }
    
    	 ShowCustomers();
    }
    
    private void btnNewCustomer_Click(object sender, System.EventArgs e)
    {
    	 NewCustomer dlgCust = new NewCustomer();
    				 
    	 if( dlgCust.ShowDialog() == DialogResult.OK )
    	 {
    		 string strFilename = "Customers.bcr";
    		 Customer cust = new Customer();
    
    		 cust.DrvLicNbr = dlgCust.txtDrvLicNbr.Text;
    		 cust.FullName  = dlgCust.txtFullName.Text;
    		 cust.Address   = dlgCust.txtAddress.Text;
    		 cust.City      = dlgCust.txtCity.Text;
    		 cust.State     = dlgCust.txtState.Text;
    		 cust.ZIPCode   = dlgCust.txtZIPCode.Text;
    		 cust.Country   = dlgCust.txtCountry.Text;
    
    		 lstCustomers.Add(cust);
    			 
     	 FileStream bcrStream = new FileStream(strFilename, 
    		FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
    		 SoapFormatter bcrSoap = new SoapFormatter();
    		 bcrSoap.Serialize(bcrStream, lstCustomers);
    		 bcrStream.Close();
    						 
    		 ShowCustomers();
    	 }
    }
    
    private void btnClose_Click(object sender, System.EventArgs e)
    {
    	Close();
    }
    }
    }
  22. Display the first form, Switchboard.cs [Design]. Add a Button to the form and change its properties as follows:
    (Name): btnCustomers
    Text: Customers
  23. Double-click the Employees button to generate its Click event
  24. Implement the event as follows:
     
    private void btnCustomers_Click(object sender, System.EventArgs e)
    {
    	Customers frmCust = new Customers();
    
    	frmCust.Show();
    }
  25. Execute the application
  26. Create a few employees as follows:
     
    Driver's Lic. # Full Name Address City State ZIP Code
    M-505-862-575 Lynda Melman 4277 Jamison Ave Silver Spring   20904
    379-82-7397 John Villard 108 Hacken Rd NE Washington DC 20012
    J-938-928-274 Chris Youno 8522 Aulage Street Rockville   20852
    K-497-220-614 Pamela Ulmreck 12075 Famina Rd Hyattsville MD 20707
    922-71-8395 Helene Kapsco 806 Hyena Drive Alexandria VA 22231
    C-374-830-422 Hermine Crasson 6255 Old Georgia Ave Silver Spring   20910
    836-55-2279 Alan Pastore 4228 16th Street NW Washington DC 20004
    B-397-597-487 Phillis Buster 724 Cranston Circle College Park   20747
    K-115-802-957 Elmus Krazucki 808 Rasters Ave Chevy Chase MD 20854
    294-90-7744 Helena Weniack 10448 Great Pollard Hwy Arlington VA 22232
  27. Close the forms and return to your programming environment

 


Previous Copyright © 2005-2016, FunctionX Next