private void DeleteContactToolStripMenuItem_Click(object sender, EventArgs e) { //check to see if the user selcted an item //before performing the delete opertaion int index = allContactListBox.SelectedIndex; if (index == -1) { MessageBox.Show("You must select an item to be deleted"); return; } //pop confirmation dialogue that the item is about to be deleted UniversityPeople person = peopleList[index]; if (DialogResult.Yes != MessageBox.Show($"Are you sure you wish to delete {person.FirstName}'s details?", "Confirmation", MessageBoxButtons.YesNo)) { return; } //delete the item and remote it from the education people list //remove it from the listbox display as well peopleList.RemoveAt(index); allContactListBox.Items.RemoveAt(index); changesMade = true; }
// edit event handller private void EditContactToolStripMenuItem_Click(object sender, EventArgs e) { Console.WriteLine("edit has been selected"); // make sure we have sth selected int index = allContactListBox.SelectedIndex; if (index == -1) { MessageBox.Show("You have to select first"); return; } UniversityPeople p = peopleList[index]; // create a new university people object if (p.Category == "Faculty") { editFaculty(index); changesMade = true; } else if (p.Category == "Student") { editStudent(index); changesMade = true; } else { MessageBox.Show("Unknow contact trying to be edited"); } }
// set person's typle private string UniversityPeopleTypeString(UniversityPeople up) { if (up is Faculty) { return("Faculty"); } else if (up is Student) { return("Student"); } else { return("Unknown"); } }
// Student in the context menu has been clicked private void studentToolStripMenuItem_Click(object sender, EventArgs e) { // create a student object, properites will be assigned in the createNewFaculty function UniversityPeople person = null; person = creatNewStudent(); // store the new student and display it in the contact list box if (person != null) { peopleList.Add(person); allContactListBox.Items.Add(person.ToFormattedString()); } changesMade = true; }
// open menu selected private void OpenToolStripMenuItem_Click(object sender, EventArgs e) { // let the user pick the file to open OpenFileDialog ofd = new OpenFileDialog(); // get the file path ofd.Title = "Select a Contact List"; ofd.Filter = "Text Files|*.txt|All Files|*.*"; ofd.FilterIndex = 1; ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // check dialog result DialogResult result = ofd.ShowDialog(); if (result != DialogResult.OK) { return; } // create a new object UniversityPeople p = null; // save the file path Filepath = ofd.FileName; try { StreamReader input = new StreamReader(ofd.FileName); while (!input.EndOfStream) { string peopleType = input.ReadLine(); switch (peopleType) { case "Faculty": p = new Faculty(input.ReadLine()); // create a new faculty break; case "Student": p = new Student(input.ReadLine()); // create a new student break; default: MessageBox.Show("Unknown product in the file"); p = null; break; } // add the new object to people list and list box if (p != null) { peopleList.Add(p); allContactListBox.Items.Add(p.ToFormattedString()); } } // close the file input.Close(); } catch (Exception excp) { MessageBox.Show($"File did not load. {excp.Message}"); return; } }