// Precondition: BookEdit, Edit menu item activated // Postcondition: The BookEdit dialog box is displayed. If data entered // are OK, file is opened private void bookToolStripMenuItem1_Click(object sender, EventArgs e) { List <LibraryItem> items; //list of items items = _lib.GetItemsList(); if (items.Count == 0) // Must have items in list { MessageBox.Show("Must have an item in the Library"); } else { BookEdit bEditForm = new BookEdit(items); //dialogbox form book DialogResult result = bEditForm.ShowDialog(); if (result == DialogResult.OK) //Only allows selection if ok { BookForm bookEditor = new BookForm(); LibraryBook item = (LibraryBook)items[bEditForm.BookIndex]; DialogResult edited = bookEditor.ShowDialog(); //Opens the BookForm to edit if (edited == DialogResult.OK) //Only edits if ok { item.Title = bookEditor.ItemTitle; item.Publisher = bookEditor.ItemPublisher; item.CopyrightYear = int.Parse(bookEditor.ItemCopyrightYear); item.LoanPeriod = int.Parse(bookEditor.ItemLoanPeriod); item.CallNumber = bookEditor.ItemCallNumber; } bookEditor.Dispose(); } } }
//Preconditions: Edit, Book menu item activated //Postconditions: Opens a dialog box to select a book to edit, and another form to edit the book private void bookToolStripMenuItem1_Click(object sender, EventArgs e) { List <LibraryItem> items; //list of items items = _lib.GetItemsList(); if (items.Count == 0) // Must have items in list { MessageBox.Show("Must have an item in the Library"); } else { LibraryItemEdit edit = new LibraryItemEdit(items); DialogResult result = edit.ShowDialog(); if (result == DialogResult.OK) //Only allows selection if ok { LibraryItem item = items[edit.ItemIndex]; BookForm editor = new BookForm(); //seperate form but same format from bookform DialogResult edited = editor.ShowDialog(); //Opens the BookForm to edit if (edited == DialogResult.OK) //Only edits if ok { item.Title = editor.ItemTitle; item.Publisher = editor.ItemPublisher; item.CopyrightYear = int.Parse(editor.ItemCopyrightYear); item.LoanPeriod = int.Parse(editor.ItemLoanPeriod); item.CallNumber = editor.ItemCallNumber; } editor.Dispose(); } } }
// Precondition: Insert, Book menu item activated // Postcondition: The Book dialog box is displayed. If data entered // are OK, a LibraryBook is created and added to the library private void bookToolStripMenuItem_Click(object sender, EventArgs e) { BookForm bookForm = new BookForm(); // The book dialog box form DialogResult result = bookForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { try { // Use form's properties to get book info to send to library _lib.AddLibraryBook(bookForm.ItemTitle, bookForm.ItemPublisher, int.Parse(bookForm.ItemCopyrightYear), int.Parse(bookForm.ItemLoanPeriod), bookForm.ItemCallNumber, bookForm.BookAuthor); } catch (FormatException) // This should never happen if form validation works! { MessageBox.Show("Problem with Book Validation!", "Validation Error"); } } bookForm.Dispose(); // Good .NET practice - will get garbage collected anyway }
private void BookToolStripMenuItem1_Click(object sender, EventArgs e) { List <LibraryItem> items; // List of items items = _lib.GetItemsList(); if (items.Count() == 0) // Must have items { MessageBox.Show("Must have books to edit!", "Edit Patron Error"); } else { SelectBookForm selectBookForm = new SelectBookForm(items); // The Select Book dialog box form load with items DialogResult result = selectBookForm.ShowDialog(); // Show form as dialog and stores result if (result == DialogResult.OK) // Only add if OK { try { BookForm bookForm = new BookForm(); // The patron dialog box form LibraryBook selected = (LibraryBook)items[selectBookForm.BookIndex]; //carryovers the selected book's properties into the new form bookForm.ItemTitle = selected.Title; // sets itemTitle property to one the user selected bookForm.ItemPublisher = selected.Publisher; // sets ItemPublisherTxt property to one the user selected bookForm.ItemCopyrightYear = selected.CopyrightYear.ToString(); // sets ItemCopyrightYearsTxt property to one the user selected bookForm.ItemLoanPeriod = selected.LoanPeriod.ToString(); // sets LoanPeriodTxt property to one the user selected bookForm.ItemCallNumber = selected.CallNumber; // sets ItemCallNumberTxt property to one user selected bookForm.BookAuthor = selected.Author; // sets bookAuthorTxt property to one user selected bookForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { selected.Title = bookForm.ItemTitle; // sets Title to input selected.Publisher = bookForm.ItemPublisher; // sets Publisher to input selected.CopyrightYear = int.Parse(bookForm.ItemCopyrightYear); // sets CopyrightYears to input selected.LoanPeriod = int.Parse(bookForm.ItemLoanPeriod); // sets Loan Period to input selected.CallNumber = bookForm.ItemCallNumber; // sets Call Number to input selected.Author = bookForm.BookAuthor; // sets Call Number to input } bookForm.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); } } selectBookForm.Dispose(); // Good .NET practice - will get garbage collected anyway } }
// Precondition: Insert, Book menu item activated // Postcondition: The Book dialog box is displayed. If data entered // are OK, a LibraryBook is created and added to the library private void bookToolStripMenuItem_Click(object sender, EventArgs e) { BookForm bookForm = new BookForm(); // The book dialog box form DialogResult result = bookForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { try { // Use form's properties to get book info to send to library lib.AddLibraryBook(bookForm.ItemTitle, bookForm.ItemPublisher, int.Parse(bookForm.ItemCopyrightYear), int.Parse(bookForm.ItemLoanPeriod), bookForm.ItemCallNumber, bookForm.BookAuthor); } catch (FormatException) // This should never happen if form validation works! { MessageBox.Show("Problem with Book Validation!", "Validation Error"); } } bookForm.Dispose(); // Good .NET practice - will get garbage collected anyway }
// Precondition: Edit, Book menu item activated // Postcondition: The book selected from the library has been edited // with the new information replacing the existing object's // properties private void bookToolStripMenuItem1_Click(object sender, EventArgs e) { List <LibraryItem> bookItems; // List of items that are books List <LibraryItem> items; // List of library items items = _lib.GetItemsList(); bookItems = new List <LibraryItem>(); for (int i = 0; i < items.Count(); ++i) { if (items[i] is LibraryBook) // Book? { bookItems.Add(items[i]); } } if ((bookItems.Count() == 0)) // Must have books to edit { MessageBox.Show("Must have books to edit!", "Edit Error"); } else { ReturnForm siForm = new ReturnForm(bookItems); // The select item dialog box form // Just reused ReturnForm, they are identical siForm.Text = "Edit Book"; // Change return form's title to match new purpose DialogResult result = siForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only edit if OK { int selectedIndex; // Index of selected item selectedIndex = siForm.ItemIndex; if (selectedIndex >= 0) // -1 if didn't select item from combo box (should never happen!) { LibraryBook editBook = bookItems[selectedIndex] as LibraryBook; // Book being edited //LibraryBook editBook = (LibraryBook)bookItems[selectedIndex]; // Book being edited // Need downcast to get to Author BookForm bookForm = new BookForm(); // The book dialog box form // Populate form fields from selected book bookForm.ItemTitle = editBook.Title; bookForm.ItemPublisher = editBook.Publisher; bookForm.ItemCallNumber = editBook.CallNumber; bookForm.ItemCopyrightYear = editBook.CopyrightYear.ToString("D4"); // ToString since property is String on form bookForm.ItemLoanPeriod = editBook.LoanPeriod.ToString(); // ToString since property is String on form bookForm.BookAuthor = editBook.Author; DialogResult editResult = bookForm.ShowDialog(); // Show form as dialog and store result if (editResult == DialogResult.OK) // Only update book if OK { // Edit book properties using form fields try { //Edits the item dictionary so that it isn't corrupted due to the keys changing //_lib.ChangeItemKey(bookItems[selectedIndex].CallNumber, editBook.CallNumber, editBook); editBook.Title = bookForm.ItemTitle; editBook.Publisher = bookForm.ItemPublisher; //editBook.CallNumber = bookForm.ItemCallNumber; //(Edited out to prevent corruption of //dictionary editBook.Author = bookForm.BookAuthor; editBook.CopyrightYear = int.Parse(bookForm.ItemCopyrightYear); editBook.LoanPeriod = int.Parse(bookForm.ItemLoanPeriod); } catch (FormatException) // This should never happen if form validation works! { MessageBox.Show("Problem with Book Validation!", "Validation Error"); } } bookForm.Dispose(); // Good .NET practice - will get garbage collected anyway } } siForm.Dispose(); // Good .NET practice - will get garbage collected anyway } }