private void openToolStripMenuItem_Click(object sender, EventArgs e) { // configure the open file dialog ContactListOpenFileDialog.InitialDirectory = Directory.GetCurrentDirectory(); // open the file dialog var openFileDialogResult = ContactListOpenFileDialog.ShowDialog(); if (openFileDialogResult != DialogResult.Cancel) { // create a new stream reader StreamReader streamReader = new StreamReader(ContactListOpenFileDialog.FileName); // read in the list while (!streamReader.EndOfStream) { streamReader.ReadLine(); ContactListBox.Items.Add(streamReader.ReadLine()); streamReader.ReadLine(); streamReader.ReadLine(); } // cleanup streamReader.Close(); } }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { // configure the open file dialog ContactListOpenFileDialog.InitialDirectory = Directory.GetCurrentDirectory(); // open the file dialog var openFileDialogResult = ContactListOpenFileDialog.ShowDialog(); if (openFileDialogResult != DialogResult.Cancel) { // create a new stream reader StreamReader streamReader = new StreamReader(ContactListOpenFileDialog.FileName); Contacts = new List <Contact>(); // instantiates a new the List container // clear content from TextBoxes ResetForm(); // read in the list from the file while (!streamReader.EndOfStream) { var contact = new Contact(); contact.FirstName = streamReader.ReadLine(); contact.LastName = streamReader.ReadLine(); contact.EmailAddress = streamReader.ReadLine(); contact.ContactNumber = streamReader.ReadLine(); ContactComboBox.Items.Add(contact.LastName); Contacts.Add(contact); // add our new contact to the Contacts List } // cleanup streamReader.Close(); ContactComboBox.SelectedIndex = 0; } }