Starting Out With Visual c# 2012 Chapter 9 - Programming Problem #7: E-Mail Addr
ID: 651320 • Letter: S
Question
Starting Out With Visual c# 2012
Chapter 9 - Programming Problem #7:
E-Mail Address Book-
Create an 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 address, 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.
Explanation / Answer
C# code:
//Header file section
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;
//name property of the Form window
namespace _9_7PP
{
//Double click the form window then the
//form is loaded
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Click the seach button
private void searchBtn_Click(object sender, EventArgs e)
{
phonebook search = new phonebook(nameTxt.Text, phNumTxt.Text, emailTxt.Text);
printTxt.Text = search.dataFound(nameTxt.Text);
}
//Click the Exit button
private void exitBtn_Click(object sender, EventArgs e)
{
Application.Exit();
}
//Click the Add button
private void addBtn_Click(object sender, EventArgs e)
{
string name;
string phNum;
string email;
name = nameTxt.Text;
phNum = phNumTxt.Text;
email = emailTxt.Text;
if (nameTxt.Text != "" && phNumTxt.Text != "" && emailTxt.Text != "")
{
person create = new person(name, phNum, email);
}
else
{
MessageBox.Show("Error: Not added values!");
}
}
//Click the Clear button
private void clearBtn_Click(object sender, EventArgs e)
{
nameTxt.Text = "";
phNumTxt.Text = "";
emailTxt.Text = "";
}
//Click the Save Button
private void saveBtn_Click(object sender, EventArgs e)
{
}
//Click the listBox
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
//Create a class PersonEntry
public class personEntry
{
//Declare variables
private string pname;
private string phNumber;
private string emailAdd;
public personEntry(string name, string phNum, string email)
{
pname = name;
phNumber = phNum;
emailAdd = email;
phonebook addContact = new phonebook(pname, phNumber, emailAdd);
}
public string name
{
get
{
return pname;
}
set
{
pname = value;
}
}
public string phNum
{
get
{
return phNumber;
}
set
{
phNumber = value;
}
}
public string email
{
get
{
return emailAdd;
}
set
{
emailAdd = value;
}
}
}
public class phonebook
{
private string Forname;
private string phNum;
private string email;
int contactCount = 0;
personEntry[] Contact = new personEntry[100];
public phonebook(string name, string phNum, string email)
{
Forname = name;
phNum = phNum;
email = email;
}
public void addContact()
{
Contact[this.contactCount] = new personEntry(Forname, phNum, email);
contactCount++;
}
public string dataFound(string name)
{
string nameFound = "";
string phoneFound = "";
string emailFound = "";
for (int i = 0; i < contactCount; i++)
{
if (Contact[i].name == name)
{
nameFound = Contact[i].name;
phoneFound = Contact[i].phNum;
emailFound = Contact[i].email;
}
}
return nameFound + phoneFound + emailFound;
}
}
}
Note: Before start the program, create windows form as per requirements and create text file.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.