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: Creating Employees
|
|
- 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 |
|
colCountry |
Country |
|
- 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 |
|
|
- To add another form to the application, on the main menu, click Project
-> Add Windows Forms
- Set the Name to NewCustomer and press Enter
- 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 |
|
- To add a new class to the project, on the main menu, click Project -> Add
Class...
- Set the Class Name to Customer and click Finish
- 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;
}
}
}
|
- In the Class View, expand BCR2 and BCR2
- Right-click Employees -> Add -> Add Method...
- Set the Method Access to internal
- Set the Return Type to void
- Set the Method Name to ShowCustomers and its Access to private
- Click Finish
- Return to the Customers form and double-click an empty area of its body to
generate its Load event
- Return to the Customers form and double-click its New Customer button
- Return to the Customers form and double-click its Close button
- 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();
}
}
}
|
- Display the first form, Switchboard.cs [Design]. Add a Button to the form and
change its properties as follows:
(Name): btnCustomers
Text: Customers
- Double-click the Employees button to generate its Click event
- Implement the event as
follows:
private void btnCustomers_Click(object sender, System.EventArgs e)
{
Customers frmCust = new Customers();
frmCust.Show();
}
|
- Execute the application
- 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 |
|
|
- Close the forms and return to your programming environment
|
|