Jump to content

Programming C#

Aaaaaaaaa

 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 YourLastName_Lab7
{
    public partial class FormMain : Form
    {
        /// <summary>
        /// Initialize the graphical user interface of the form
        /// </summary>
        public FormMain()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// Saves the contact to a file
        /// </summary>
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            // validate required fields
            string name = this.textBoxName.Text.Trim();
            string address = this.textBoxAddress.Text.Trim();
 
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(address))
            {
                MessageBox.Show("The name and address is required.");
                return;
            }
 
            // Open a file and save it
            StreamWriter sw = new StreamWriter("addresses.txt", true);
 
            sw.WriteLine(name);
            sw.WriteLine(address + "\n");
 
            sw.Flush();
            sw.Close();
 
            // Display the result in the richtextbox
            this.richTextBoxContacts.AppendText(name + "\n" + address + "\n\n");
 
            this.textBoxName.Clear();
            this.textBoxAddress.Clear();
        }
    }
}
 

 

 

Above is my code for this question below! But for some reason I am having problems executing. Can someone please review it and see what is wrong. Thank You!

 

 

 

 

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.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
Link to comment
Share on other sites

Link to post
Share on other sites

Looks like it should work, assuming you only want to do the first bunch of steps in your pseudo code. You've forgotten the part where it opens the stream for reading. Also, if you aren't required to use the StreamReader and read it line by line, you can do the following:

StreamReader sr = new StreamReader("addresses.txt");this.richTextBoxContacts.AppendText(sr.ReadToEnd());sr.close()
Link to comment
Share on other sites

Link to post
Share on other sites

 

Looks like it should work, assuming you only want to do the first bunch of steps in your pseudo code. You've forgotten the part where it opens the stream for reading. Also, if you aren't required to use the StreamReader and read it line by line, you can do the following:

StreamReader sr = new StreamReader("addresses.txt");this.richTextBoxContacts.AppendText(sr.ReadToEnd());sr.close()

Where should I insert  what you have mentioned in my code?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×