In: Computer Science
Must be in C#: 7. E-MAIL ADDRESS BOOK Create a Windows Forms Application with a class named PersonEntry. The PersonEntry class should have properties for a person’s name, e-mail address, and phone number. Also, create a text file that contains the names, e-mail addresses, and phone numbers for at least five people. When the application starts, it should read the data from the file and create a PersonEntry object for each person’s data. The PersonEntry objects should be added to a List, and each person’s name should be displayed in a list box on the application’s main form. When the user selects a name from the list box, a second form should appear displaying that person’s name, e-mail address, and phone number. There are additional requirements; please see Main Differences between the Textbook Instructions and This Document above.
SourceCode:
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;
using System.IO;
namespace PhoneBook
{
public partial class Form1 : Form
{
//create a List object of PersonEntry type
List<PersonEntry> entries = new List<PersonEntry>();
public Form1()
{
InitializeComponent();
}
//create a class called PersonEntry
class PersonEntry
{
//declare the required class variables
public string name;
public string email;
public string phone_num;
//define the constructor of the class
public PersonEntry()
{
name = "";
email = "";
phone_num = "";
}
//setter methods
public void setName(string pname)
{
name = pname;
}
public void setEmail(string pemail)
{
email = pemail;
}
public void setPhone(string phone)
{
phone_num = phone;
}
//getter methods
public string getName()
{
return name;
}
public string getEmail()
{
return email;
}
public string getPhone()
{
return phone_num;
}
}
//when the create list button is click the following
//is done
private void listbtn_Click(object sender, EventArgs e)
{
//declare a string variable and a delimiter
string line;
char delimiter = '\t';
//create an object to StreamReader
StreamReader readfile = new StreamReader(@"C:\Users\soujanya.m\Documents\Visual Studio 2010\Projects\PhoneBook\PhoneBook\PhoneBook.txt");
//loop until end of stream
while (!readfile.EndOfStream)
{
//read each line from the file
line = readfile.ReadLine();
//split the line at give delimiter
string[] parse = line.Split(delimiter);
//create an object to the PersonEntry class
PersonEntry perEntry = new PersonEntry();
//set the values
perEntry.setName(parse[0]);
perEntry.setEmail(parse[1]);
perEntry.setPhone(parse[2]);
//add the object to the list called entries
entries.Add(perEntry);
//add the names to the list box
listBox1.Items.Add(perEntry.getName());
}
}
//logic to close the form on clicking the exit button
private void exitbtn_Click(object sender, EventArgs e)
{
Close();
}
//when the items are selected, a new form will be opened
//and displays the respective details of the name selected
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Form newform = new Form();
int index = listBox1.SelectedIndex;
newform.Text = entries[index].getName() + " details: ";
string str = "";
Label lb = new Label();
//add the string values to display
str += "Name: " + entries[index].getName() + "\n";
str += "Email address: " + entries[index].getEmail() + "\n";
str += "Phone number: " + entries[index].getPhone() + "\n";
//set the size and font of the label
lb.Font = new Font("Serif", 12, FontStyle.Bold);
lb.Size = new System.Drawing.Size(250, 250);
//add the string to the label
lb.Text = str;
lb.Visible = true;
lb.Location = new System.Drawing.Point(10, 10);
//add the label to the newform
newform.Controls.Add(lb);
//set the size of the new form
newform.Size = new System.Drawing.Size(300, 200);
newform.ShowDialog();
}
}
}
Sample Input and Output: