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 Employees.
/// </summary>
public class Employees : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnNewEmployee;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.ColumnHeader colLastName;
private System.Windows.Forms.ColumnHeader colFirstName;
private System.Windows.Forms.ColumnHeader colTitle;
private System.Windows.Forms.ColumnHeader colGender;
private System.Windows.Forms.ListView lvwEmployees;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
ArrayList lstEmployees;
. . . No Change
internal void ShowEmployees()
{
SoapFormatter bcrSoap = new SoapFormatter();
String strFilename = "Employees.bcr";
if( File.Exists(strFilename) )
{
FileStream bcrStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
ArrayList lstEmpl = (ArrayList)bcrSoap.Deserialize(bcrStream);
bcrStream.Close();
Employee empl;
lvwEmployees.Items.Clear();
for(int i = 0; i < lstEmpl.Count; i++)
{
empl = (Employee)lstEmpl[i];
ListViewItem lviEmployee = new ListViewItem(empl.FirstName);
lviEmployee.Font = new Font("Georgia", 8, FontStyle.Bold);
if( i % 2 == 0 )
{
lviEmployee.BackColor = Color.Blue;
lviEmployee.ForeColor = Color.White;
}
else
{
lviEmployee.BackColor = Color.LightBlue;
lviEmployee.ForeColor = Color.Blue;
}
lviEmployee.SubItems.Add(empl.LastName);
lviEmployee.SubItems.Add(empl.Title);
lviEmployee.SubItems.Add(empl.Gender);
lvwEmployees.Items.Add(lviEmployee);
}
}
}
private void Employees_Load(object sender, System.EventArgs e)
{
lstEmployees = new ArrayList();
SoapFormatter bcrSoap = new SoapFormatter();
string strFilename = "Employees.bcr";
if( File.Exists(strFilename) )
{
FileStream bcrStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
lstEmployees = (ArrayList)bcrSoap.Deserialize(bcrStream);
bcrStream.Close();
}
ShowEmployees();
}
private void btnNewEmployee_Click(object sender, System.EventArgs e)
{
NewEmployee dlgEmpl = new NewEmployee();
if( dlgEmpl.ShowDialog() == DialogResult.OK )
{
if( dlgEmpl.txtLastName.Text == "" )
{
MessageBox.Show("You must provide at least a last name to create a new employee");
return;
}
else
{
string strFilename = "Employees.bcr";
Employee empl = new Employee();
empl.FirstName = dlgEmpl.txtFirstName.Text;
empl.LastName = dlgEmpl.txtLastName.Text;
empl.Title = dlgEmpl.txtTitle.Text;
empl.Gender = dlgEmpl.cboGenders.Text;
lstEmployees.Add(empl);
FileStream bcrStream = new FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
SoapFormatter bcrSoap = new SoapFormatter();
bcrSoap.Serialize(bcrStream, lstEmployees);
bcrStream.Close();
ShowEmployees();
}
}
}
private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
}
}
|