Create an application with a class named PersonEntry. The PersonEntry class shou
ID: 3779321 • Letter: C
Question
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 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.Explanation / Answer
{
class PersonEntry
{
public string PersonName { get; set; } //Creates a variable for the person's name.
public string PersonEmail { get; set; } //Creates a variable for the person's email.
public string PersonPhoneNumber { get; set; } //Creates a variable for the person's phone number.
}
}
+++++++++++
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
namesListBox.Items.Clear(); //Clears the Listbox.
}
private void getNamesButton_Click(object sender, EventArgs e)
{
//Field to hold a list of PersonEntry objects.
List<PersonEntry> nameList =
new List<PersonEntry>();
//Create an instance of the PersonEntry structure.
PersonEntry entry = new PersonEntry();
try
{
StreamReader sr = new StreamReader(@"C:\PhoneEmailDataList.txt");
string line;
while (!sr.EndOfStream)
{
//namesListBox.Items.Add(line);
line = sr.ReadLine();
//Identify the delimiter.
char[] delim = { ',' };
//Tokenize the line.
string[] tokens = line.Split(delim);
//Store the tokens in the entry object.
entry.PersonName = tokens[0];
entry.PersonEmail = tokens[1];
entry.PersonPhoneNumber = tokens[2];
//Add the entry object to the List.
namesListBox.Items.Add(entry.PersonName);
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR: YOU ARE A DUMBASS " + ex.Message);
}
}
private void DisplayNameList()
{
//Add the entry objects to the List
foreach (PersonEntry nameDisplay in nameList)
{
namesListBox.Items.Add(nameDisplay.PersonName);
}
}
public void namesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (namesListBox.SelectedIndex != -1)
{
//Get full info for the selected item in the list
string name = nameList[namesListBox.SelectedIndex].PersonName;
string email = nameList[namesListBox.SelectedIndex].PersonEmail;
string phone = nameList[namesListBox.SelectedIndex].PersonPhoneNumber;
//Create second form for these
DetailForm f2 = new DetailForm(name, email, phone);
f2.ShowDialog();
}
else
{
MessageBox.Show(" pick a name, or what?");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.