// When the user presses a key, determine whether to add a new student to a class, remove a student from a class, or modify the details of a student private void studentsList_KeyDown(object sender, KeyEventArgs e) { switch (e.Key) { // If the user pressed Enter, edit the details for the currently selected student case Key.Enter: Student student = this.studentsList.SelectedItem as Student; // Use the StudentsForm to display and edit the details of the student StudentForm sf = new StudentForm(); // Set the title of the form and populate the fields on the form with the details of the student sf.Title = "Edit Student Details"; sf.firstName.Text = student.FirstName; sf.lastName.Text = student.LastName; sf.dateOfBirth.Text = student.DateOfBirth.ToString("d"); // Format the date to omit the time element // Display the form if (sf.ShowDialog().Value) { // When the user closes the form, copy the details back to the student student.FirstName = sf.firstName.Text; student.LastName = sf.lastName.Text; student.DateOfBirth = DateTime.Parse(sf.dateOfBirth.Text); // Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; // If the user pressed Insert, add a new student case Key.Insert: // Use the StudentsForm to get the details of the student from the user sf = new StudentForm(); // Set the title of the form to indicate which class the student will be added to (the class for the currently selected teacher) sf.Title = "New Student for Class " + teacher.Class; // Display the form and get the details of the new student if (sf.ShowDialog().Value) { // When the user closes the form, retrieve the details of the student from the form // and use them to create a new Student object Student newStudent = new Student(); newStudent.FirstName = sf.firstName.Text; newStudent.LastName = sf.lastName.Text; newStudent.DateOfBirth = DateTime.Parse(sf.dateOfBirth.Text); // Assign the new student to the current teacher this.teacher.Students.Add(newStudent); // Add the student to the list displayed on the form this.studentsInfo.Add(newStudent); // Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; } }
// When the user presses a key, determine whether to add a new student to a class, remove a student from a class, or modify the details of a student private void studentsList_KeyDown(object sender, KeyEventArgs e) { switch (e.Key) { // If the user pressed Enter, edit the details for the currently selected student case Key.Enter: Student student = this.studentsList.SelectedItem as Student; // Use the StudentsForm to display and edit the details of the student StudentForm sf = new StudentForm(); // Set the title of the form and populate the fields on the form with the details of the student sf.Title = "Edit Student Details"; sf.firstName.Text = student.FirstName; sf.lastName.Text = student.LastName; sf.dateOfBirth.Text = student.DateOfBirth.ToString("d"); // Format the date to omit the time element // Display the form if (sf.ShowDialog().Value) { // When the user closes the form, copy the details back to the student student.FirstName = sf.firstName.Text; student.LastName = sf.lastName.Text; student.DateOfBirth = DateTime.Parse(sf.dateOfBirth.Text); // Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; } }
// When the user presses a key, determine whether to add a new student to a class, remove a student from a class, or modify the details of a student private void studentsList_KeyDown(object sender, KeyEventArgs e) { Student student; StudentForm sf = new StudentForm(); switch(e.Key) { case Key.Enter: // TODO: Exercise 1: Task 1a: If the user pressed Enter, edit the details for the currently selected student student = this.studentsList.SelectedItem as Student; sf.Title = "Edit Student Details"; sf.firstName.Text = student.FirstName; sf.lastName.Text = student.LastName; sf.dateOfBirth.Text = student.DateOfBirth.ToString("d"); if(sf.ShowDialog().Value) { student.FirstName = sf.firstName.Text; student.FirstName = sf.lastName.Text; sf.dateOfBirth.Text = student.DateOfBirth.ToString("d"); saveChanges.IsEnabled = true; } break; case Key.Insert: student = new Student(); break; case Key.Delete: student = this.studentsList.SelectedItem as Student; break; // TODO: Exercise 1: Task 2a: Use the StudentsForm to display and edit the details of the student // TODO: Exercise 1: Task 2b: Set the title of the form and populate the fields on the form with the details of the student // TODO: Exercise 1: Task 3a: Display the form // TODO: Exercise 1: Task 3b: When the user closes the form, copy the details back to the student // TODO: Exercise 1: Task 3c: Enable saving (changes are not made permanent until they are written back to the database) } }
// When the user presses a key, determine whether to add a new student to a class, remove a student from a class, or modify the details of a student private void studentsList_KeyDown(object sender, KeyEventArgs e) { switch (e.Key) { // If the user pressed Enter, edit the details for the currently selected student case Key.Enter: Student student = this.studentsList.SelectedItem as Student; // Use the StudentsForm to display and edit the details of the student StudentForm sf = new StudentForm(); // Set the title of the form and populate the fields on the form with the details of the student sf.Title = "Edit Student Details"; sf.firstName.Text = student.FirstName; sf.lastName.Text = student.LastName; sf.dateOfBirth.Text = student.DateOfBirth.ToString("d"); // Format the date to omit the time element // Display the form if (sf.ShowDialog().Value) { // When the user closes the form, copy the details back to the student student.FirstName = sf.firstName.Text; student.LastName = sf.lastName.Text; student.DateOfBirth = DateTime.Parse(sf.dateOfBirth.Text); // Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; // If the user pressed Insert, add a new student case Key.Insert: // Use the StudentsForm to get the details of the student from the user sf = new StudentForm(); // Set the title of the form to indicate which class the student will be added to (the class for the currently selected teacher) sf.Title = "New Student for Class " + teacher.Class; // Display the form and get the details of the new student if (sf.ShowDialog().Value) { // When the user closes the form, retrieve the details of the student from the form // and use them to create a new Student object Student newStudent = new Student(); newStudent.FirstName = sf.firstName.Text; newStudent.LastName = sf.lastName.Text; newStudent.DateOfBirth = DateTime.Parse(sf.dateOfBirth.Text); // Assign the new student to the current teacher this.teacher.Students.Add(newStudent); // Add the student to the list displayed on the form this.studentsInfo.Add(newStudent); // Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; // If the user pressed Delete, remove the currently selected student case Key.Delete: student = this.studentsList.SelectedItem as Student; // Prompt the user to confirm that the student should be removed MessageBoxResult response = MessageBox.Show( String.Format("Remove {0}", student.FirstName + " " + student.LastName), "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No); // If the user clicked Yes, remove the student from the database if (response == MessageBoxResult.Yes) { this.schoolContext.Students.DeleteObject(student); // Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; } }
// When the user presses a key, determine whether to add a new student to a class, remove a student from a class, or modify the details of a student private void studentsList_KeyDown(object sender, KeyEventArgs e) { switch (e.Key) { // If the user pressed Enter, edit the details for the currently selected student #region Enter Key Press case Key.Enter: Student student = this.studentsList.SelectedItem as Student; // Use the StudentsForm to display and edit the details of the student StudentForm sf = new StudentForm(); // Set the title of the form and populate the fields on the form with the details of the student sf.Title = "Edit Student Details"; sf.firstName.Text = student.FirstName; sf.lastName.Text = student.LastName; sf.dateOfBirth.Text = student.DateOfBirth.ToString("d"); // Format the date to omit the time element // Display the form if (sf.ShowDialog().Value) { // When the user closes the form, copy the details back to the student student.FirstName = sf.firstName.Text; student.LastName = sf.lastName.Text; student.DateOfBirth = DateTime.Parse(sf.dateOfBirth.Text); // Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; #endregion // If the user pressed Insert, add a new student #region Insert Key Press case Key.Insert: // Use the StudentsForm to get the details of the student from the user sf = new StudentForm(); // Set the title of the form to indicate which class the student will be added to (the class for the currently selected teacher) sf.Title = "New Student for Class " + teacher.Class; // Display the form and get the details of the new student if (sf.ShowDialog().Value) { // When the user closes the form, retrieve the details of the student from the form // and use them to create a new Student object Student newStudent = new Student(); newStudent.FirstName = sf.firstName.Text; newStudent.LastName = sf.lastName.Text; newStudent.DateOfBirth = DateTime.Parse(sf.dateOfBirth.Text); // Assign the new student to the current teacher this.teacher.Students.Add(newStudent); // Add the student to the list displayed on the form this.studentsInfo.Add(newStudent); // Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; #endregion // TODO: Exercise 3: Task 1a: If the user pressed Delete, remove the currently selected student #region Delete Key Press case Key.Delete: student = studentsList.SelectedItem as Student; // TODO: Exercise 3: Task 2a: Prompt the user to confirm that the student should be removed MessageBoxResult resultMessageBox = MessageBox.Show( string.Format($"Remove {student.FirstName} {student.LastName}"), "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No); // TODO: Exercise 3: Task 3a: If the user clicked Yes, remove the student from the database if (resultMessageBox == MessageBoxResult.Yes) { schoolContext.DeleteObject(student); // TODO: Exercise 3: Task 3b: Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; #endregion } }
// When the user presses a key, determine whether to add a new student to a class, remove a student from a class, or modify the details of a student private void studentsList_KeyDown(object sender, KeyEventArgs e) { // Use the StudentsForm to display and edit the details of the student StudentForm sf = new StudentForm(); Student student = this.studentsList.SelectedItem as Student; switch (e.Key) { // If the user pressed Enter, edit the details for the currently selected student #region Key Enter Press case Key.Enter: // Set the title of the form and populate the fields on the form with the details of the student sf.Title = "Edit Student Details"; sf.firstName.Text = student.FirstName; sf.lastName.Text = student.LastName; sf.dateOfBirth.Text = student.DateOfBirth.ToString("d"); // Format the date to omit the time element // Display the form if (sf.ShowDialog().Value) { // When the user closes the form, copy the details back to the student student.FirstName = sf.firstName.Text; student.LastName = sf.lastName.Text; student.DateOfBirth = DateTime.Parse(sf.dateOfBirth.Text); // Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; #endregion #region Key Insert Press case Key.Insert: // TODO: Exercise 2: Task 1a: If the user pressed Insert, add a new student // TODO: Exercise 2: Task 2a: Use the StudentsForm to get the details of the student from the user sf = new StudentForm(); // TODO: Exercise 2: Task 2b: Set the title of the form to indicate which class the student will be added to (the class for the currently selected teacher) sf.Title = "New Student for Class " + teacher.Class; // TODO: Exercise 2: Task 3a: Display the form and get the details of the new student if (sf.ShowDialog().Value) // TODO: Exercise 2: Task 3b: When the user closes the form, retrieve the details of the student from the form and use them to create a new Student object { Student newstudent = new Student(); newstudent.FirstName = sf.firstName.Text; newstudent.LastName = sf.lastName.Text; newstudent.DateOfBirth = DateTime.Parse(sf.dateOfBirth.Text); // TODO: Exercise 2: Task 4a: Assign the new student to the current teacher this.teacher.Students.Add(newstudent); // TODO: Exercise 2: Task 4b: Add the student to the list displayed on the form this.studentsInfo.Add(newstudent); // TODO: Exercise 2: Task 4c: Enable saving (changes are not made permanent until they are written back to the database) saveChanges.IsEnabled = true; } break; #endregion } }