|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
- To add a new class to the project, in the Class View, right-click BethesdaCarRental1 -> Add -> Class...
- Set the Class Name to Customer and click Finish
- Change the Customer.cs file as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BethesdaCarRental1 { [Serializable] public class Customer { public string FullName; public string Address; public string City; public string State; public string ZIPCode; // This constructor defines a customer public Customer(string fName = "John Unknown", string adr = "123 Main Street", string ct = "Great City", string ste = "AA", string zip = "00000-0000") { FullName = fName; Address = adr; City = ct; State = ste; ZIPCode = zip; } } }
- To add another form to the application, on the main menu, click Project -> Add Windows Forms
- Set the Name to CustomerEditor and click Add
- Design the form as follows:
Control Text Name Properties Label Driver's Lic. #: TextBox txtDrvLicNbr Modifiers: Public Label State: ComboBox cbxStates Modifiers: Public
Items: AL, AK, AZ, AR, CA, CO, CT, DE, DC, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA, WV, WI, WYLabel Full Name: TextBox txtFullName Modifiers: Public Label Address: TextBox txtAddress Modifiers: Public Label City: TextBox txtCity Modifiers: Public Label ZIP Code: TextBox txtZIPCode Modifiers: Public Button OK btnOK DialogResult: OK Button Close btnCancel DialogResult: Cancel Form AcceptButton: btnOK
CancelButton: btnCancel
FormBorderStyle: FixedDialog
MaximizeBox: False
MinimizeBox: False
ShowInTaskbar: False - To add a new form to the application, on the main menu, click Project -> Add Windows Forms
- Set the Name to Customers and press Enter
- From the Toolbox, add a ListView to the form
- 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 - Design the form as follows:
Control Text Name Other Properties ListView lvwCustomers View: Details
GridLines: True
FullRowSelect: TrueButton New Customer... btnNewCustomer Anchor: Bottom, Right Button Close btnClose Anchor: Bottom, Right - Double-click an unoccupied area of the form
- Change the file as follows:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace BethesdaCarRental1 { public partial class Customers : Form { Dictionary<string, Customer> ListOfCustomers; public Customers() { InitializeComponent(); } internal void ShowCustomers() { if (ListOfCustomers.Count == 0) return; lvwCustomers.Items.Clear(); int i = 1; foreach (KeyValuePair<string, Customer> kvp in ListOfCustomers) { ListViewItem lviCustomer = new ListViewItem(kvp.Key); Customer cust = kvp.Value; 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); if (i % 2 == 0) { lviCustomer.BackColor = Color.Navy; lviCustomer.ForeColor = Color.White; } else { lviCustomer.BackColor = Color.Blue; lviCustomer.ForeColor = Color.White; } lvwCustomers.Items.Add(lviCustomer); i++; } } private void Customers_Load(object sender, EventArgs e) { ListOfCustomers = new Dictionary<string, Customer>(); BinaryFormatter bfmCustomers = new BinaryFormatter(); string strFilename = @"\\EXPRESSION\Bethesda Car Rental\Customers.crc"; if (File.Exists(strFilename)) { FileStream stmCustomers = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read); try { // Retrieve the list of employees from file ListOfCustomers = (Dictionary<string, Customer>)bfmCustomers.Deserialize(stmCustomers); } finally { stmCustomers.Close(); } } ShowCustomers(); } } }
- Return to the Customers form and double-click the New Customer button
- Implement its event as follows:
private void btnNewCustomer_Click(object sender, EventArgs e) { CustomerEditor editor = new CustomerEditor(); if (editor.ShowDialog() == DialogResult.OK) { if (editor.txtDrvLicNbr.Text == "") { MessageBox.Show("You must provide the driver's " + "license number of the customer", "Bethesda Car Rental", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (editor.txtFullName.Text == "") { MessageBox.Show("You must provide the employee's full name", "Bethesda Car Rental", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string strFilename = @"\\EXPRESSION\Bethesda Car Rental\Customers.crc"; Customer cust = new Customer(); cust.FullName = editor.txtFullName.Text; cust.Address = editor.txtAddress.Text; cust.City = editor.txtCity.Text; cust.State = editor.cbxStates.Text; cust.ZIPCode = editor.txtZIPCode.Text; ListOfCustomers.Add(editor.txtDrvLicNbr.Text, cust); FileStream bcrStream = new FileStream(strFilename, FileMode.Create, FileAccess.Write, FileShare.Write); BinaryFormatter bcrBinary = new BinaryFormatter(); bcrBinary.Serialize(bcrStream, ListOfCustomers); bcrStream.Close(); ShowCustomers(); } }
- Display the Central form
- Double-click the Customers button and implement its event as
follows:
private void btnCustomers_Click(object sender, EventArgs e) { Customers dlgCustomers = new Customers(); dlgCustomers.ShowDialog(); }
- To save the project, on the Standard toolbar, click the Save All button
No comments:
Post a Comment