Address Book

This is an object-oriented window forms program using C#. It is a simple address book that allows you to add manage and delete contacts.

[code lang=”csharp”]
///——————————————————
/// Cynthia Chadwick
/// C# Final
/// 12/08/2009
///
/// Program allows a user to add, delete, and save
/// contacts to a .mcl file that can be loaded.
///——————————————————
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CynthiaChadwickFinalExam
{
public partial class CS233SFinal : Form
{
// List of contacts
public List<Contact> ContactList = new List<Contact>();
public const string FileName = "Contacts.mcl"; // name of file

public CS233SFinal()
{
InitializeComponent();
}

/*****************************************************************
* opens newContact form and creates a new contact
******************************************************************/
private void NewButton_Click(object sender, EventArgs e)
{
NewContact newContact = new NewContact();
newContact.NewContactUpdated +=
new NewContact.NewContactHandler(NewContact_ButtonClick);
newContact.Show();
}

/*****************************************************************
* takes the new contact and adds it to the list
******************************************************************/
private void NewContact_ButtonClick(object sender, Contact contact)
{
AddContact(contact);
}

/*****************************************************************
* deletes selected contact
******************************************************************/
private void DeleteButton_Click(object sender, EventArgs e)
{
int index = 0;

try
{
index = Convert.ToInt32(ContactsTreeView.SelectedNode.Name);
}
catch
{
ContactInfoTextBox.Text = "";
WarningException warning = new WarningException
("failed to parse index for tree view item. Index string:");
return;
}
ContactList.RemoveAt(index);
ContactsTreeView.SelectedNode.Remove();
}

/*****************************************************************
* save button saves to file
******************************************************************/
private void SaveButton_Click(object sender, EventArgs e)
{
Save();
}

/*****************************************************************
* saves contact list to overwriten file
******************************************************************/
private void Save()
{
using (TextWriter SaveContacts = new StreamWriter(FileName))
{
for (int i = 0; i < ContactList.Count; i++)
{
Contact SaveContact = ContactList[i];

string NewContact = string.Format("{0},{1},{2},{3},{4},{5},{6},{7};",
SaveContact.LastName, SaveContact.FirstName, SaveContact.Address,
SaveContact.City, SaveContact.State, SaveContact.ZipCode,
SaveContact.HomePhone, SaveContact.CellPhone);

SaveContacts.WriteLine(NewContact);
}
}
}

/*****************************************************************
* cleans the program and loads the file and fills the treeview
* and contact list
******************************************************************/
private void LoadButton_Click(object sender, EventArgs e)
{
Clear(); // clears the program
TreeViewFile(FileName);
// loads the file and fills the treeview and contact list
}

/*****************************************************************
* clears the contact list and the treeview
******************************************************************/
private void Clear()
{
ContactList.Clear(); // clears the contact list of values
for (int i = 0; i < ContactsTreeView.Nodes.Count; i++) {
ContactsTreeView.Nodes[i].Nodes.Clear(); // clears the tree view of values
}
}
public string[] Contacts; public string[] ViewContacts;
/*****************************************************************
* reads info from the file and splits it into contact strings
******************************************************************/
private void TreeViewFile(string file)
{
try
{
// open textreader
using (TextReader ContactFile = new StreamReader(file))
{
// read file and split it into contact
strings Contacts = ContactFile.ReadToEnd().Split(‘;’);
foreach (string str in Contacts)
{
// skip the lines that only contain white space
string Str = str.Trim();
if (Str != "")
{
// parse the line and add to contact AddContact(ParseContact(Str));
}
}
}
}
catch { return; }
}

/*****************************************************************
* adds contact to treeview and to list
******************************************************************/
private void AddContact(Contact contact)
{
try
{
ContactList.Add(contact);
char FirstLetter = Convert.ToChar(Convert.ToString (contact.LastName[0]).ToUpper());
if (FirstLetter >= ‘A’ && FirstLetter <= ‘Z’)
{
ContactsTreeView.Nodes[FirstLetter – ‘A’].Nodes.Add(
(ContactList.Count – 1).ToString(),
string.Format("{0}, {1}", contact.LastName,
contact.FirstName));
}
}
catch
{
return;
}
}

/*****************************************************************
* parses out the contact string
******************************************************************/
private Contact ParseContact(string info)
{
Contact contact = new Contact();

string[] parts = info.Split(‘,’);

contact.LastName = parts[0].Trim();
contact.FirstName = parts[1].Trim();
contact.Address = parts[2].Trim();
contact.City = parts[3].Trim();
contact.State = parts[4].Trim();
contact.ZipCode = parts[5].Trim();
contact.HomePhone = parts[6].Trim();
contact.CellPhone = parts[7].Trim();

return contact;
}

/*****************************************************************
* shows the contact info in the info window when
* you select a contact
******************************************************************/
private void ContactsTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
int index = 0;
try
{
index = Convert.ToInt32(e.Node.Name);
}
catch
{
ContactInfoTextBox.Text = "";
return;
}

ContactInfoTextBox.Text = string.Format(
"{0}, {1}" + Environment.NewLine +
"{2}" + Environment.NewLine +
"{3}, {4} {5}" + Environment.NewLine +
"Home Phone: {6,13}" + Environment.NewLine +
"Cell Phone: {7,13}", ContactList[index].LastName, ContactList[index].FirstName,
ContactList[index].Address, ContactList[index].City,
ContactList[index].State, ContactList[index].ZipCode,
ContactList[index].HomePhone, ContactList[index].CellPhone);
}
}
}
[/code]