Your mission: Write a Windows application that gets names and e-mail addresses f
ID: 3647894 • Letter: Y
Question
Your mission:Write a Windows application that gets names and e-mail addresses from the user and displays a list of all of the user's contacts. Each time the user enters a new contact, he/she should able to see it added to a list of previously stored contacts. For example, in the following sample output below, after the user clicks the "Add Contact" button, the list will include John Mann's contact information at the bottom.
Sample output:
Tips
Best practice:
First, make sure to read through this week's lecture and textbook readings to get ideas about how to work with files. Then, before attempting a GUI application, just try implementing a short console application to open an existing .txt file, add some lines to it, and then close it. Use Windows Explorer to navigate to the file, open it, and see if your program worked. If it did, then try to open the .txt file from your console app and read data from it. Once you have that working, try the same operations in the form of a GUI app. Refer to the textbook for useful examples.
Note: Make sure to first create a .txt file in your project's bin/Debug folder to hold the contact info.
When designing the GUI, consider initially disabling the e-mail textbox and the button until all information is entered. Redisable them when the button is clicked. Note that you can also use the Tab Index property of GUI objects to set their order of use.
Pseudocode:
When the "Add Contact" button is clicked
Open an output file stream for appending
Write the contact's name and e-mail address to the file
Close the file stream
Clear the list
Open an input file stream for reading
Add each line of the file to the list
Close the file stream
When the form closes Close the input and output file streams
Your mission:Write a Windows application that gets names and e-mail addresses from the user and displays a list of all of the user's contacts. Each time the user enters a new contact, he/she should able to see it added to a list of previously stored contacts. For example, in the following sample output below, after the user clicks the "Add Contact" button, the list will include John Mann's contact information at the bottom.
Sample output:
Tips
Best practice:
First, make sure to read through this week's lecture and textbook readings to get ideas about how to work with files. Then, before attempting a GUI application, just try implementing a short console application to open an existing .txt file, add some lines to it, and then close it. Use Windows Explorer to navigate to the file, open it, and see if your program worked. If it did, then try to open the .txt file from your console app and read data from it. Once you have that working, try the same operations in the form of a GUI app. Refer to the textbook for useful examples.
Note: Make sure to first create a .txt file in your project's bin/Debug folder to hold the contact info.
When designing the GUI, consider initially disabling the e-mail textbox and the button until all information is entered. Redisable them when the button is clicked. Note that you can also use the Tab Index property of GUI objects to set their order of use.
Pseudocode:
When the "Add Contact" button is clicked
Open an output file stream for appending
Write the contact's name and e-mail address to the file
Close the file stream
Clear the list
Open an input file stream for reading
Add each line of the file to the list
Close the file stream
When the form closes Close the input and output file streams
Explanation / Answer
I'll assume you're working in VS2010 and you know how to create a windows form in c# with similar controls. label1 = Name label2 = Email Address label3 = CONTACTS TextBox1 = the textbox field for the name ?TextBox2 = the textbox field for the email address TextBox3 - set multiline=true == the textbox streached out to display the list of contacts button1 = Add Contact Events Handling Go under the Events list for the Form and double click the FormClosing event to generate ? Form1_FormClosing(object sender, FormClosingEventArgs e) Go to the button button1 and double-click it to generate the its Click event: ?? private void button1_Click(object sender, EventArgs e) This is for the disabling/enabling textbox 2 and addContact button handling Go to the textboxes events list, and generate the TextChanged event for each one: private void textBox1_TextChanged(object sender, EventArgs e) private void textBox2_TextChanged(object sender, EventArgs e) Now, Go to your form class in code. Here is what it can look like. I create and call a private method to write stuff to the file and clear the Name and Email textboxes (you can remove that if you don't need it). Which I call first in the button click event. And I created and call another private method to read from the file, clear the multiline textbox and fill it out with contacts info. In the constructor, I set my Textbox2 and button to (Enabled = false) initialize my filestreams and helpers appropriately for reading and writing. The text file I use is contacts.txt and if it doesn't exist it is created for you under your VS application bin/Debug folder. In the form_closing event handler method - I close my file streams and helpers properly. 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; namespace ContactManager { public partial class Form1 : Form { private System.IO.FileStream outputFileStream; private System.IO.StreamWriter streamWriter; private System.IO.FileStream inputFileStream; private System.IO.StreamReader streamReader; public Form1() { InitializeComponent(); this.textBox1.Enabled = false; this.button1.Enabled = false; this.outputFileStream = new System.IO.FileStream(@"contacts.txt", System.IO.FileMode.Append); this.streamWriter = new System.IO.StreamWriter(inputFileStream); this.inputFileStream = new System.IO.FileStream(@"contacts.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read); this.streamReader = new System.IO.StreamReader(inputFileStream); this.updateContactList(); } private void button1_Click(object sender, EventArgs e) { // writes to / updates the contact file this.updateContactFile(); // reads from file / updates the GUI list this.updateContactList(); } private void updateContactFile() { string name = this.textBox1.Text; string email = this.textBox2.Text; try { streamWriter.WriteLine(name); streamWriter.WriteLine(email); } catch (Exception exp) { Console.WriteLine(exp.Message); } this.textBox1.Clear(); this.textBox2.Clear(); } private void updateContactList() { this.textBox3.Clear(); try { while (streamReader.EndOfStream == false) { string name = streamReader.ReadLine(); string email = streamReader.ReadLine(); string contact = name + Environment.NewLine + email + Environment.NewLine + Environment.NewLine; this.textBox3.AppendText(contact); } } catch (Exception exp) { Console.WriteLine(exp.Message); } } private void textBox1_TextChanged(object sender, EventArgs e) { if (this.textBox1.Text.Length > 0) this.textBox2.Enabled = true; else this.textBox2.Enabled = false; } private void textBox2_TextChanged(object sender, EventArgs e) { if (this.textBox2.Text.Length > 0) this.button1.Enabled = true; else this.button1.Enabled = false; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { this.streamReader.Close(); this.streamWriter.Close(); this.inputFileStream.Close(); this.outputFileStream.Close(); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.