#pragma once
#using <System.Runtime.Serialization.Formatters.Soap.dll>
#include "NewEmployee.h"
#include "Employee.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Soap;
namespace BCR2
{
. . .
public __gc class Employees : public System::Windows::Forms::Form
{
public:
. . .
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container* components;
ArrayList *lstEmployees;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
. . .
}
void ShowEmployees(void)
{
SoapFormatter *bcrSoap = new SoapFormatter();
String *strFilename = S"Employees.bcr";
if( File::Exists(strFilename) )
{
FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
ArrayList *lstEmpl = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
bcrStream->Close();
Employee* empl;
lvwEmployees->Items->Clear();
for(int i = 0; i < lstEmpl->Count; i++)
{
empl = dynamic_cast<Employee *>(lstEmpl->Item[i]);
ListViewItem *lviEmployee = new ListViewItem(empl->FirstName);
lviEmployee->Font = new Drawing::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: System::Void Employees_Load(System::Object * sender, System::EventArgs * e)
{
lstEmployees = new ArrayList;
SoapFormatter *bcrSoap = new SoapFormatter();
String *strFilename = S"Employees.bcr";
if( File::Exists(strFilename) )
{
FileStream *bcrStream = new FileStream(strFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
lstEmployees = dynamic_cast<ArrayList *>(bcrSoap->Deserialize(bcrStream));
bcrStream->Close();
}
ShowEmployees();
}
private: System::Void btnNewEmployee_Click(System::Object * sender, System::EventArgs * e)
{
NewEmployee *dlgEmpl = new NewEmployee;
if( dlgEmpl->ShowDialog() == DialogResult::OK )
{
if( dlgEmpl->txtLastName->Text->Equals(S"") )
{
MessageBox::Show(S"You must provide at least a last name to create a new employee");
return;
}
else
{
String *strFilename = S"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: System::Void btnClose_Click(System::Object * sender, System::EventArgs * e)
{
Close();
}
};
}
|