/// <summary> /// Add Faculty member submenu option; adds faculty member to contact list /// </summary> /// <param name="sender">Method parameter for the component that fires the event</param> /// <param name="e">Method parameter for the events tied to the component</param> private void FacultyToolStripMenuItem_Click(object sender, EventArgs e) { AddEditFaculty AddFaculty = new AddEditFaculty(); //create instance of addeditfaculty window DialogResult result = AddFaculty.ShowDialog(this); //variable the stores result returned from modal dialog form; shows add faculty form if (result == DialogResult.OK) { //create instance of student class with the newly added student info FacultyPerson addedFaculty = new FacultyPerson(AddFaculty.addedFirstName, AddFaculty.addedLastName, AddFaculty.addedAcademicDepartment, AddFaculty.addedEmailAddress, AddFaculty.addedAddress, "Faculty"); ContactListBox.Items.Add(addedFaculty.ToFormattedString()); //display the info in our student list mainContactList.Add(addedFaculty); //add faculty person to our contact list saveChanges = true; //update public boolean variable that indicates some changes have been made so prompt will be given on exit } AddFaculty.Dispose(); }
/// <summary> /// Open file menu option; opens contact list file and populates main contact info list box /// </summary> /// <param name="sender">Method parameter for the component that fires the event</param> /// <param name="e">Method parameter for the events tied to the component</param> private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (ContactListBox.Items.Count > 0) { DialogResult saveBeforeOpen = MessageBox.Show("Would You like to Save the current data before loading new data?", "Save Before Open", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); //If we have any data in our contact list and attempt to open a file, prompt the user if they want to save that data if (saveBeforeOpen == DialogResult.Yes) { saveContactsToolStripMenuItem_Click(sender, e); } else if (saveBeforeOpen == DialogResult.Cancel) { return; } } OpenFileDialog ofd = new OpenFileDialog(); //Create open file dialog popup ofd.Title = "Select a contact list"; //Set title of open file dialog popup ofd.Filter = "Text Files|*.txt|All Files|*.*"; //create filter of open file dialog popup ofd.FilterIndex = 1; //set filter index of open file dialog popup ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //set initial directory of open file dialog popup DialogResult result = ofd.ShowDialog(); //set return value of popup dialog to variable; shows open file dialog popup //If a file was not opened close dialog popup if (result != DialogResult.OK) { return; } ContactListBox.Items.Clear(); //clear contact listbox before opening file mainContactList.Clear(); //clear contact list before opening file // all inside a try/catch // // open a stream reader on contact list file 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 mainFilePath = ofd.FileName; //Grab the file path and assign it to public variable for saving UniversityPerson uPerson = null; //create null class instance variable try { StreamReader input = new StreamReader(ofd.FileName);//create stream reader object for reading file contents //While loop that reads lines of file while (!input.EndOfStream) { string personType = input.ReadLine(); //variable to store line (all a persons details) from file string[] items = personType.Split('|'); //put each line into an array to check //Create a variable for each persons info string filePersonType = items[0]; string fileFirstName = items[1]; string fileLastName = items[2]; string fileAcademicDepartment = items[3]; string fileEmailAddress = items[4]; string fileAddress = items[5]; List <string> fileCoursesList = new List <string>(); //switch that creates instances of each person based on their type read from the file switch (filePersonType) { case "Student": string fileGraduationYear = items[6]; string fileCourseList = items[7]; List <string> convertedCourseList = fileCourseList.Split(',').Select(x => x).ToList(); //take the course list items into an array; split my string by commas, select each value and turn into a list //Create instance of student with values selected from file uPerson = new Student(fileFirstName, fileLastName, fileAcademicDepartment, fileEmailAddress, fileAddress, int.Parse(fileGraduationYear), filePersonType, convertedCourseList); break; case "Faculty": //Create instance of student with values selected from file uPerson = new FacultyPerson(fileFirstName, fileLastName, fileAcademicDepartment, fileEmailAddress, fileAddress, filePersonType); break; default: MessageBox.Show("Unknown person in the file"); uPerson = null; break; } //If we have a valid person add them to our contact list box and contact list; set the changes saved boolean to false as this is an open file if (uPerson != null) { mainContactList.Add(uPerson); ContactListBox.Items.Add(uPerson.ToFormattedString()); saveChanges = false; } } input.Close();//close stream reader object } catch (Exception ex) { MessageBox.Show($"File was unable to load. {ex.Message}", ex.GetType().Name); return; } }
/// <summary> /// Edit contact menu option; edits contact from contact list /// </summary> /// <param name="sender">Method parameter for the component that fires the event</param> /// <param name="e">Method parameter for the events tied to the component</param> private void editContactToolStripMenuItem_Click(object sender, EventArgs e) { int index = ContactListBox.SelectedIndex;//variable that stores selected index of contact list box try { //Prompts user to select a contact if none is selected; edits contact from contact list box and list if one is selected, does not allow selecting more than one if (index == -1) { MessageBox.Show("You must select person to edit"); } else if (ContactListBox.SelectedItems.Count > 1) { MessageBox.Show("You must select one contact at a time to edit"); return; } else { //Checks what the type of person being selected for editing is then populates their respective modal dialog form for editing if (mainContactList[index].PersonType == "Student") { AddEditStudent editStudent = new AddEditStudent(); //create instance of addeditstudent window editStudent.EditMode = true; //enable edit mode //create instance of student class utlizing the selected index Student personToEdit = (Student)mainContactList[index]; //update form fields with values from contact list for selected person editStudent.addedFirstName = personToEdit.FirstName; editStudent.addedLastName = personToEdit.LastName; editStudent.addedAcademicDepartment = personToEdit.AcademicDepartment; editStudent.addedEmailAddress = personToEdit.EmailAddress; editStudent.addedAddress = personToEdit.Address; editStudent.addedGraduationYear = personToEdit.ExpectedGraduationYear.ToString(); //editStudent.CoursesList = personToEdit.CourseList; editStudent.CoursesList = new List <string>(personToEdit.CourseList); DialogResult result = editStudent.ShowDialog();// show the dialog and wait for an OK. // if answer was OK update the contact list with the new values and update display if (result != DialogResult.OK) { return; } //create an instance of student class initialized with updated values Student updatedStudent = new Student(editStudent.addedFirstName, editStudent.addedLastName, editStudent.addedAcademicDepartment, editStudent.addedEmailAddress, editStudent.addedAddress, int.Parse(editStudent.addedGraduationYear), "Student", editStudent.CoursesList); mainContactList[index] = updatedStudent; //Populate contact list with updated values ContactListBox.Items[index] = updatedStudent.ToFormattedString(); //populate contact listbox with updated values saveChanges = true; //update public boolean variable that indicates some changes have been made so prompt will be given on exit } else if (mainContactList[index].PersonType == "Faculty") { AddEditFaculty editFaculty = new AddEditFaculty(); //create instance of addeditfaculty window editFaculty.EditMode = true; //enable edit mode //create instance of student class utlizing the selected index FacultyPerson personToEdit = (FacultyPerson)mainContactList[index]; //update form fields with values from contact list for selected person editFaculty.addedFirstName = personToEdit.FirstName; editFaculty.addedLastName = personToEdit.LastName; editFaculty.addedAcademicDepartment = personToEdit.AcademicDepartment; editFaculty.addedEmailAddress = personToEdit.EmailAddress; editFaculty.addedAddress = personToEdit.Address; DialogResult result = editFaculty.ShowDialog();// show the dialog and wait for an OK. // if answer was OK update the contact list with the new values and update display if (result != DialogResult.OK) { return; } //create an instance of faculty person class initialized with updated values FacultyPerson updatedFaculty = new FacultyPerson(editFaculty.addedFirstName, editFaculty.addedLastName, editFaculty.addedAcademicDepartment, editFaculty.addedEmailAddress, editFaculty.addedAddress, "Faculty"); mainContactList[index] = updatedFaculty; //Populate contact list with updated values ContactListBox.Items[index] = updatedFaculty.ToFormattedString(); //populate contact listbox with updated values saveChanges = true; //update public boolean variable that indicates some changes have been made so prompt will be given on exit editFaculty.Dispose(); } } } catch (Exception ex) { //string msg = "You must select a person before editing their details"; //string variable message; warning message //string capt = "Edit Contact Information";//string variable caption; warning caption //initiate an instance of DialogResult class with return value of CustomMsgBoxes method call MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Information); } }