Sunday, May 6, 2012

Final Project Completed

I've finished the final project.  I can see why some of my classmates had trouble with it.  I don't know if passing data between Form objects is covered in the book or not...I stopped reading at around Chapter 3, other than to get the details for weekly assignments.

If anyone got stuck, here is the code for the project...so you can at least see how it is done (or rather, how I implemented it).

Here is the class in Form1 (frmMain in my project). Be sure to add a using statement for System.IO in order to get StreamReader/StreamWriter operational.
public partial class frmMain : Form
    {
        public string[] pbEntries = new string[5];
        public List People = new List();

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            ReadTextFile();
            AddToListBox();
        }

        public void ReadTextFile()
        {

            int index = 0;                                                          // Index used in loops.
            StreamReader inputFile;                                                 // Declare StreamReader.
            inputFile = File.OpenText("myPhoneBook.txt");                           // Open file and get SR obj.

            while (index < pbEntries.Length && !inputFile.EndOfStream)
            {
                pbEntries[index] = inputFile.ReadLine();                            // Read contents of myPhoneBook.txt, store in pbEntries String array.
                index++;
            }
            
            for (int count = 0; count < pbEntries.Length; count++)
            {
                ConvertToObject(pbEntries[count]);                                  // Read each item in the String array 
            }
            inputFile.Close();                                                      // Close the file.
        }

        public string ConvertToObject(string dataLine)
        {
            string[] pInfo = dataLine.Split(',');                                   // Split entry for each person into sub-entries in the pInfo String array.
            PersonEntry Person = new PersonEntry(pInfo[0], pInfo[1], pInfo[2]);     // New instance of PersonEntry, pass name, email, and phone fields.
            People.Add(Person);                                                     // Add new Person object to People<> List.
            return null;
        }

        public void AddToListBox()
        {
            for (int count = 0; count != People.Count; count++)
            {
                lbPhoneBook.Items.Add(People[count].Name);                          // Durrr...obvious.
            }
        }

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

        private void lbPhoneBook_SelectedIndexChanged(object sender, EventArgs e)
        {
            frmSub Form = new frmSub(People[lbPhoneBook.SelectedIndex]);            // Overload new frmSub, passed PersonEntry object in People<>
            Form.ShowDialog();                                                      //  ...with SelectedIndex of lbPhoneBook.
        }   // No idea why it's throwing these  tags here...that's not part of my project code, it's something to do with the code coloring script.

And here is the code for the second form's code.
public frmSub(PersonEntry thisPerson)
        {
            InitializeComponent();
            lblName.Text = thisPerson.Name;
            lblEmail.Text = thisPerson.Email;
            lblPhone.Text = thisPerson.Phone;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

And the required PersonEntry class for the project itself:

public class PersonEntry
    {
        // Fields
        private string _Name;
        private string _Email;
        private string _Phone;

        // Constructor
        public PersonEntry(string Name, string Email, string Phone)
        {
            _Name = Name;
            _Email = Email;
            _Phone = Phone;
        }

        // Name property
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        // Email property
        public string Email
        {
            get { return _Email; }
            set { _Email = value; }
        }

        // Phone property
        public string Phone
        {
            get { return _Phone; }
            set { _Phone = value; }
        }
    }

No comments:

Post a Comment