Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# Program Project: I am getting an error on loading my file and getting it to output correctly. The assignment is Create a project that

C# Program Project:

I am getting an error on loading my file and getting it to output correctly. The assignment is

"Create a project that stores personal information for a little electronic black book. The fields in the file should include name, phone number, pager number, cell phone number, voice mail number, and e-mail address. Use text boxes to enter the data and a menu system that will allow the user to load the "black book". Add another form to load the names into a list box and use a structure to hold the fields of data. Perform a look up and display the appropriate information for the selected name also Allow the user to select the file to open using the Open File dialog box."

Here is Form 1's Code

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO;

namespace Chapter11Excercise { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void addButton_Click(object sender, EventArgs e) { string strName = textbox1.Text; string strPhoNo = textbox2.Text; string strPageNumber = textBox3.Text; string strCPhNumber = textBox4.Text; string strVoiceMailNumb = textBox5.Text; string strEMail = textBox6.Text;

string strFileName = "blackbook.txt";

string inputData = strName + "," + strPhoNo + "," + strCPhNumber + "," + strVoiceMailNumb + "," + strEMail + "," + Environment.NewLine; if (File.Exists(strFileName)) { File.AppendAllText(strFileName, inputData); } else { File.WriteAllText(strFileName, inputData); } textbox1.Text = ""; textbox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = "";

}

private void exitToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); }

private void loadBlackBookToolStripMenuItem_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.ShowDialog(); } } }

Here is Form 1s Image:

image text in transcribed

Here is Form 2 Code:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO;

namespace Chapter11Excercise { struct bookDetails { public string Name; public string PhNo; public string PageNumber; public string CellPhoneNumber; public string VoiceMailNumber; public string EMail; }

public partial class Form2 : Form { List lst = new List(); public Form2() { InitializeComponent(); string strFileName = "blackbook.txt"; string[] lines = File.ReadAllLines(strFileName);

foreach (string strLine in lines) { bookDetails bt = new bookDetails(); bt.Name = strLine.Split(',')[0]; bt.PhNo = strLine.Split(',')[1]; bt.PageNumber = strLine.Split(',')[2]; bt.CellPhoneNumber = strLine.Split(',')[3]; bt.VoiceMailNumber = strLine.Split(',')[4]; bt.EMail = strLine.Split(',')[5];

lst.Add(bt);

blackBookListBox.Items.Add(strLine.Split(',')[0]); } }

private void blackBookListBox_SelectedIndexChanged(object sender, EventArgs e) { string strName = blackBookListBox.SelectedItem.ToString();

bookDetails dt = lst.Where(x => x.Name == strName).FirstOrDefault();

label1.Text = "Name : " + dt.Name; label2.Text = "PhoneNumber : " + dt.PhNo; label3.Text = "Page Number : " + dt.PageNumber; label4.Text = "Cell Ph No : " + dt.CellPhoneNumber; label5.Text = "Voice email : " + dt.VoiceMailNumber; label6.Text = "voice Ph No : " + dt.EMail; } } }

Here is frm 2 image:

image text in transcribed

It will not allow form 2 to load because of a out of range exception I am getting.

Thank You

Black Book File Black Book Contact Information Name Phone Number: Pager Number Cell Phone Number Voicemail Number E-Ma Add

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions

Question

2. What process will you put in place to address conflicts?

Answered: 1 week ago