//Pre Condition: invoked when Patron, Edit menu is activated // Post Condition: The file is opened private void patronToolStripMenuItem1_Click(object sender, EventArgs e) { List <LibraryPatron> patrons; // List of patrons patrons = _lib.GetPatronsList(); if ((patrons.Count() == 0)) // Must have patrons { MessageBox.Show("Must have patrons to edit!", "Edit Patron Error"); } else { SelectPatronForm selectPatronForm = new SelectPatronForm(patrons); // The Select Patron dialog box form DialogResult result = selectPatronForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { try { PatronForm patronForm = new PatronForm(); // The patron dialog box form LibraryPatron selected = patrons[selectPatronForm.PatronIndex]; //carry over the selection into the form patronForm.PatronName = selected.PatronName; // sets Forms Patron Name property to one the user selects patronForm.PatronID = selected.PatronID; // sets Form's Patron Id property to one the user selects patronForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { // Uses the forms properties to edit patron selected.PatronName = patronForm.PatronName; // set patron's name to user's input selected.PatronID = patronForm.PatronID; // set patron's id to user's input } patronForm.Dispose(); // Good .NET practice - will get garbage collected anyway } catch (ArgumentOutOfRangeException) { MessageBox.Show("Enter Input into the Proper Field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (InvalidOperationException) { MessageBox.Show("Invalid Operation", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (FormatException) { MessageBox.Show("Please Enter Correct Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } selectPatronForm.Dispose(); // Good .NET practice - will get garbage collected anyway } }
// Precondition: Edit, Patron menu item activated // Postcondition: The patron selected from the library has been edited // with the new information replacing the existing object's // properties private void patronToolStripMenuItem1_Click(object sender, EventArgs e) { List <LibraryPatron> patrons; // List of patrons patrons = _lib.GetPatronsList(); if ((patrons.Count() == 0)) // Must have checked out items { MessageBox.Show("Must have patrons to edit!", "Edit Error"); } else { SelectPatronForm spForm = new SelectPatronForm(patrons); // The select patron dialog box form DialogResult result = spForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only edit if OK { int editIndex; // Index of patron to edit editIndex = spForm.PatronIndex; if (editIndex >= 0) // -1 if didn't select item from combo box (should never happen!) { LibraryPatron editPatron = patrons[editIndex]; // The patron being edited PatronForm patronForm = new PatronForm(); // The patron dialog box form // Populate form fields from selected patron patronForm.PatronName = editPatron.PatronName; patronForm.PatronID = editPatron.PatronID; DialogResult editResult = patronForm.ShowDialog(); // Show form as dialog and store result if (editResult == DialogResult.OK) // Only update patron if OK { //Edits the dictionary so that it isn't corrupted due to the keys changing //_lib.ChangePatronKey(patrons[editIndex].PatronID, editPatron.PatronID, editPatron); // Edit patron properties using form fields editPatron.PatronName = patronForm.PatronName; //editPatron.PatronID = patronForm.PatronID; //(Edited out to prevent corruption of dictionary) } patronForm.Dispose(); // Good .NET practice - will get garbage collected anyway } } spForm.Dispose(); // Good .NET practice - will get garbage collected anyway } }