// Function - Edit faculty staff contact information private void EditFacultyPerson(int index, int mode) { // create a dialog and configure for edit or only display AddEditFacultyDialog adfd = new AddEditFacultyDialog(); switch (mode) { case 0: // 0 - show mode case 2: // 2 - edit mode Faculty f = (Faculty)personList[index]; // assign values in personList to public properties in AddEditFacultyDialog adfd.FacultyEditMode = mode; adfd.FacultyFirstName = f.FirstName; adfd.FacultyLastName = f.LastName; adfd.FacultyAcademicDept = f.Department; adfd.FacultyEmail = f.ContactFaculty.Email; adfd.FacultyBuilding = f.ContactFaculty.Building; break; case 1: // 1 - add mode adfd.FacultyEditMode = mode; break; } // show the dialog and wait for a ok DialogResult result = adfd.ShowDialog(); // if answer was ok update the contact information with the new values and update display // update faculty contact information if (result == DialogResult.OK) { ContactFaculty facultyInfo; try { facultyInfo = new ContactFaculty(adfd.FacultyEmail, adfd.FacultyBuilding); } catch (Exception excp) { MessageBox.Show($"email address error. {excp.Message}"); return; } Person p = new Faculty(adfd.FacultyFirstName, adfd.FacultyLastName, adfd.FacultyAcademicDept, facultyInfo); // set mode to public property p.Type = "Faculty"; switch (mode) { case 0: // show mode break; case 2: // edit mode // update new values for dispaly and list personList[index] = p; personListListBox.Items[index] = p.ToFormattedString(); EditCount++; // EditCount add 1 after sucessful edit break; case 1: //add mode // add to list personList.Add(p); // add to list box for display personListListBox.Items.Add(p.ToFormattedString()); EditCount++; // EditCount add 1 after sucessful add break; default: break; } } else if (result == DialogResult.Cancel) { return; } else { MessageBox.Show("AddEditFacultyDialog not return OK ....debug"); return; } }
// Event - open file private void openToolStripMenuItem_Click(object sender, EventArgs e) { Console.WriteLine("User selected the open menu items"); // clear listbox personListListBox.Items.Clear(); // get the file path OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Open Contact Information"; ofd.Filter = "Text File|*.txt|All files|*.*"; ofd.FilterIndex = 1; if (ofd.ShowDialog() != DialogResult.OK) { return; } // open a stream reader on 'contactinformation.txt' on the desktop // for each line in the file call the constructor that takes single string // and get a object back. Add that object to my list and to the display list // close the file savePath = ofd.FileName; // get file path ready for saving file Person p = null; try { StreamReader input = new StreamReader(ofd.FileName); while (!input.EndOfStream) { string personType = input.ReadLine(); switch (personType) { case "FACULTY": p = new Faculty(input.ReadLine()); p.Type = "Faculty"; break; case "STUDENT": p = new Student(input.ReadLine()); p.Type = "Student"; break; default: MessageBox.Show("unknown person in the file"); p = null; break; } if (p != null) { personList.Add(p); personListListBox.Items.Add(p.ToFormattedString()); } } input.Close(); } catch (Exception excp) { MessageBox.Show($"File did not load. {excp.Message}"); return; } }
// Responds to Open menu item in the Main Form - Selects a file to open private void openToolStripMenuItem_Click(object sender, EventArgs e) { // let the user pick the file to open contactsListBox.Items.Clear(); OpenFileDialog ofd = new OpenFileDialog(); //Open file dialog box ofd.Title = "Select a Contact List"; ofd.Filter = "Contact List Files|*.pil|Text Files|*.txt|All Files|*.*"; ofd.FilterIndex = 1; ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // Desktop selected as the initial file directory DialogResult result = ofd.ShowDialog(); if (result != DialogResult.OK) { return; } // all inside a try/catch // // open a stream reader on 'productlist.txt' on the desktop // for each line in the file call the constructor that takes single string // and get a product object back. Add that object to my list and to the display list // close the file Member m = null; try { StreamReader input = new StreamReader(ofd.FileName); while (!input.EndOfStream) { string memberType = input.ReadLine(); switch (memberType) { case "Faculty": m = new Faculty(input.ReadLine()); break; case "Student": m = new Student(input.ReadLine()); break; default: MessageBox.Show("Unknown member in the file"); m = null; break; } if (m != null) { memberList.Add(m); contactsListBox.Items.Add(m.ToDisplayString()); } } openFileName = ofd.FileName; //Stores the filename in a variable that can be used to directly save the file input.Close(); } catch (Exception excp) { MessageBox.Show($"File did not load. {excp.Message}"); return; } }