//This .cs file creates the Form which displays the list of addresses. //Provides options to add, edit and delete addresses. //Loads the saved addresses into the listview. //Saves the addresses into a flat file while exiting. namespace AddressBook { using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using System.IO; //AddressHeader is being derived from the ColumnHeader class class AddressHeader : ColumnHeader { public AddressHeader(String myText , int myWidth , HorizontalAlignment myAlign) { this.Text = myText; this.Width = myWidth; this.TextAlign = myAlign; } } //AddressButton is erived from the Button class. class AddressButton : Button { public AddressButton(String myText) { this.Text = myText; } public void SetParams(Size mySize , Point myPoint , int myTabIndex) { this.Size = mySize; this.Location = myPoint; this.TabIndex = myTabIndex; } } public class AddressList : Form { private AddressButton btnAddButton; private AddressButton btnEditButton; private AddressButton btnRemoveButton; private AddressButton btnExitButton; private ListView lvwAddressList; private AddressHeader FirstName; private AddressHeader LastName; private AddressHeader EMailAddress; private AddressHeader PhoneNumber; public AddressList() { this.Text = "Address Book Version 1.0"; this.ClientSize = new Size(424 , 257); this.StartPosition = FormStartPosition.CenterScreen; this.FormBorderStyle = FormBorderStyle.FixedDialog; this.MaximizeBox = false; FirstName = new AddressHeader("First Name" , 75 , HorizontalAlignment.Left); LastName = new AddressHeader("Last Name" , 75 , HorizontalAlignment.Left); EMailAddress = new AddressHeader("E - Mail" , 75 , HorizontalAlignment.Left); PhoneNumber = new AddressHeader("Phone Number" , 75 , HorizontalAlignment.Left); lvwAddressList = new ListView(); lvwAddressList.Location = new Point(16,16); lvwAddressList.Size = new Size(296 , 208); lvwAddressList.FullRowSelect = true; lvwAddressList.GridLines = true; lvwAddressList.HideSelection = false; lvwAddressList.Sorting = SortOrder.Ascending; lvwAddressList.View = View.Details; //Adding the 4 ColumnHeaders using the AddRange method //Alternatively you can also use Add method lvwAddressList.Columns.AddRange(new ColumnHeader[]{FirstName , LastName , EMailAddress , PhoneNumber}); lvwAddressList.DoubleClick += new EventHandler(onDoubleClick); this.Controls.Add(lvwAddressList); btnAddButton = new AddressButton("Add"); btnAddButton.SetParams(new Size(88,32) , new Point(320,16) , 1); btnAddButton.Click += new EventHandler(onAdd); this.Controls.Add(btnAddButton); btnEditButton = new AddressButton("Edit"); btnEditButton.SetParams(new Size(88,32) , new Point(320,64) , 2); btnEditButton.Click += new EventHandler(onEdit); this.Controls.Add(btnEditButton); btnRemoveButton = new AddressButton("Remove"); btnRemoveButton.SetParams(new Size(88,32) , new Point(320,112) , 3); btnRemoveButton.Click += new EventHandler(onRemove); this.Controls.Add(btnRemoveButton); btnExitButton = new AddressButton("Exit"); btnExitButton.SetParams(new Size(88,32) , new Point(320,160) , 3); btnExitButton.Click += new EventHandler(onExit); this.Controls.Add(btnExitButton); LoadEntries(); //Opens the data file and loads the addresses into the ListView } private void LoadEntries() { try { //Using an object of the StreamReader class to open the data file. StreamReader myFileReader = File.OpenText(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\AddressBook.data"); try { String myEntry; do { myEntry = myFileReader.ReadLine(); if(myEntry != null) { ListViewItem myItem = new ListViewItem(myEntry); myItem.SubItems.Add(myFileReader.ReadLine()); myItem.SubItems.Add(myFileReader.ReadLine()); myItem.SubItems.Add(myFileReader.ReadLine()); lvwAddressList.Items.Add(myItem); } } while(myEntry != null); } catch(Exception ReadExcept) { MessageBox.Show(ReadExcept.Message , "Error reading entries" , MessageBoxButtons.OK , MessageBoxIcon.Error); } finally { myFileReader.Close(); } } catch(Exception FileOpenExcept) { MessageBox.Show("No entries exist in your address book" , "Empty address book" , MessageBoxButtons.OK , MessageBoxIcon.Exclamation); } } private void SaveData() { try { StreamWriter myFileWriter = File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\AddressBook.data"); try { for(int lsCount=0;lsCount 0) { if (MessageBox.Show("Are you sure to remove the selected item(s) ?","Remove entry/entries ?",MessageBoxButtons.YesNo , MessageBoxIcon.Question) == DialogResult.Yes) { for (int lvwCount = lvwAddressList.Items.Count;lvwCount > 0; lvwCount--) { if (lvwAddressList.Items[lvwCount-1].Selected == true) { lvwAddressList.Items[lvwCount-1].Remove(); } } } } } protected void onExit(Object mySender , EventArgs myArgs) { DialogResult myResult; myResult = MessageBox.Show("Are you sure to quit ?","Address Book 1.0",MessageBoxButtons.YesNo , MessageBoxIcon.Question); if (myResult == DialogResult.Yes) { this.Close(); Application.Exit(); // all forms are unloaded, resources freed. } } //Override the OnClosing event and save the data before closing the form protected override void OnClosing (CancelEventArgs e) { SaveData(); } } }