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;
namespace Address_Book
{
public partial class Form1 : Form
{
// members...
public AddressBook Addresses;
private int _currentAddressIndex;
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
// create a new address object...
Address address = new Address();
// copy the values from the form into the address...
PopulateAddressFromForm(address);
// save the address...
String filename = DataFileName;
address.Save(filename);
// tell the user...
MessageBox.Show("The address was saved to " + filename);
}
// DataFilename - where should we store our data?
public String DataFileName
{
get
{
// get our working folder...
String folder;
folder = Environment.CurrentDirectory;
// return the folder with the name "Addressbook.xml"...
return folder + "\\AddressBook.xml";
}
}
// PopulateAddressFromForm - populates Address from the form fields...
public void PopulateAddressFromForm(Address address)
{
// copy the values...
address.FirstName = txtFirstName.Text;
address.LastName = txtLastName.Text;
address.CompanyName = txtCompanyName.Text;
address.Address1 = txtAddress1.Text;
address.Address2 = txtAddress2.Text;
address.City = txtCity.Text;
address.Region = txtRegion.Text;
address.PostalCode = txtPostalCode.Text;
address.Country = txtCountry.Text;
address.Email = txtEmail.Text;
}
private void btnLoad_Click(object sender, EventArgs e)
{
// load the address using a shared method on SerializableData...
Address newAddress = (Address)SerializableData.Load(DataFileName,typeof(Address));
// update the display...
PopulateFormFromAddress(newAddress);
}
// PopulateFormFromAddress - populates the form from an
// address object...
public void PopulateFormFromAddress(Address address)
// copy the values...
{
txtFirstName.Text = address.FirstName;
txtLastName.Text = address.LastName;
txtCompanyName.Text = address.CompanyName;
txtAddress1.Text = address.Address1;
txtAddress2.Text = address.Address2;
txtCity.Text = address.City;
txtRegion.Text = address.Region;
txtPostalCode.Text = address.PostalCode;
txtCountry.Text = address.Country;
txtEmail.Text = address.Email;
}
private void lnkSendEmail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// Start the email client...
System.Diagnostics.Process.Start("mailto: " + txtEmail.Text);
}
// CurrentAddress - property for the current address...
Address CurrentAddress
{
get
{
return AddressBook.Items[CurrentAddressIndex - 1];
}
}
// CurrentAddressIndex - property for the current address...
int CurrentAddressIndex
{
get
{
return _currentAddressIndex;
}
set
{
// set the address...
_currentAddressIndex = Value;
// update the display...
PopulateFormFromAddress(CurrentAddress);
// set the label...
lblAddressNumber.Text = _currentAddressIndex +" of " + AddressBook.Items.Count;
}
}
private void Form1_Load(object sender, EventArgs e)
{
// load the address book...
Addresses = (AddressBook)SerializableData.Load(DataFileName,typeof(AddressBook));
// if the address book contains no item, add a new one...
if (Addresses.Items.Count == 0)
Addresses.AddAddress();
// select the first item in the list...
CurrentAddressIndex = 1;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
// save the changes...
UpdateCurrentAddress();
SaveChanges();
}
// SaveChanges - save the address book to an XML file...
public void SaveChanges()
{
// tell the address book to save itself...
Addresses.Save(DataFileName);
}
// UpdateCurrentAddress - make sure the book has the current
// values currently entered into the form...
private void UpdateCurrentAddress()
{
PopulateAddressFromForm(CurrentAddress);
}
private void btnNew_Click(object sender, EventArgs e)
{
AddNewAddress();
}
public Address AddNewAddress()
{
// save the current address...
UpdateCurrentAddress();
// create a new address...
Address newAddress = Addresses.AddAddress();
// update the display...
CurrentAddressIndex = Addresses.Items.Count;
// return the new address...
return newAddress;
}
private void btnNext_Click(object sender, EventArgs e)
{
MoveNext();
}
private void MoveNext()
{
// get the next index...
int newIndex = CurrentAddressIndex + 1;
if (newIndex > Addresses.Items.Count)
newIndex = 1;
// save any changes...
UpdateCurrentAddress();
// move the record...
CurrentAddressIndex = newIndex;
}
private void btnPrevious_Click(object sender, EventArgs e)
{
MovePrevious();
}
private void MovePrevious()
{
// get the previous index...
int newIndex = CurrentAddressIndex - 1;
if (newIndex == 0)
newIndex = Addresses.Items.Count;
// save changes...
UpdateCurrentAddress();
// move the record...
CurrentAddressIndex = newIndex;
}
private void btnDelete_Click(object sender, EventArgs e)
{
// ask the user if they are ok with this?
if (MessageBox.Show("Are you sure you want to delete this address?","Address Book",MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==DialogResult.Yes)
{
DeleteAddress(CurrentAddressIndex);
}
}
// DeleteAddress - delete an address from the list...
public void DeleteAddress(int index)
{
// delete the item from the list...
Addresses.Items.RemoveAt(index - 1);
// was that the last address?
if (Addresses.Items.Count == 0)
// add a new address?
Addresses.AddAddress();
else
// make sure you have something to show...
if (index > Addresses.Items.Count)
index = Addresses.Items.Count;
// display the record...
CurrentAddressIndex = index;
}
}
}
#################################
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Address_Book
{
public class Address : SerializableData
{
// Members...
public String FirstName;
public String LastName;
public String CompanyName;
public String Address1;
public String Address2;
public String City;
public String Region;
public String PostalCode;
public String Country;
public String Email;
}
}
####################################
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml.Serialization;
namespace Address_Book
{
class AddressBook : SerializableData
{
// members...
[XmlIgnore()]
public ArrayList Items = new ArrayList();
// AddAddress - add a new address to the book...
public Address AddAddress()
{
// create one...
Address newAddress = new Address();
// add it to the list...
Items.Add(newAddress);
// return the address...
return newAddress;
}
// Addresses - property that works with the items
// collection as an array...
public Address[] Addresses
{
get
{
// create a new array...
Address[] addressArray = new Address[Items.Count];
Items.CopyTo(addressArray);
return addressArray;
}
set
{
// reset the arraylist...
Items.Clear();
// did you get anything?
if (value != null)
{
// go through the array and populate items...
foreach (Address address in value)
{
Items.Add(address);
}
}
}
}
}
}
###############################################
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace Address_Book
{
public class SerializableData
{
// Save - serialize the object to disk...
public void Save(String filename)
{
// make a temporary filename...
String tempFilename;
tempFilename = filename + ".tmp";
// does the file exist?
FileInfo tempFileInfo = new FileInfo(tempFilename);
if (tempFileInfo.Exists)
tempFileInfo.Delete();
// open the file...
FileStream stream = new FileStream(tempFilename,FileMode.Create);
// save the object...
Save(stream);
// close the file...
stream.Close();
// remove the existing data file and
// rename the temp file...
tempFileInfo.CopyTo(filename, true);
tempFileInfo.Delete();
}
// Save - actually perform the serialization...
public void Save(Stream stream)
{
// create a serializer...
XmlSerializer serializer = new XmlSerializer(typeof(Address));
// save the file...
serializer.Serialize(stream, this);
}
// Load - deserialize from disk...
public static Object Load(String filename, Type newType)
{
// does the file exist?
FileInfo fileInfo = new FileInfo(filename);
if (!fileInfo.Exists)
{
// create a blank version of the object and return that...
return System.Activator.CreateInstance(newType);
}
// open the file...
FileStream stream = new FileStream(filename, FileMode.Open);
// load the object from the stream...
Object newObject = Load(stream, newType);
// close the stream...
stream.Close();
// return the object...
return newObject;
}
public static Object Load(Stream stream, Type newType)
{
// create a serializer and load the object....
XmlSerializer serializer = new XmlSerializer(newType);
Object newObject = serializer.Deserialize(stream);
// return the new object...
return newObject;
}
}
}