Beispiel #1
0
        // 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();
                }
            }
        }
        private void bookToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            List<LibraryItem> items = new List<LibraryItem>();  //list for all library items
            List<LibraryItem> books = new List<LibraryItem>();  //list for library books
            List<int> bookIndices;      // List of index values of items checked out

            items = lib.GetItemsList(); //get current list of library items
            bookIndices = new List<int>(); //list for book indicies

            for (int i = 0; i < items.Count(); i++) //loop to end of list
            {
                if (items[i] is LibraryBook) //if the item type is LibraryBook
                {
                    books.Add(items[i]); //add book to list
                    bookIndices.Add(i); //add item index
                }
            }

            BookEdit bookEditForm = new BookEdit(books); //form to choose the book to edit

            DialogResult result = bookEditForm.ShowDialog(); //show form with selected book data

            if (result == DialogResult.OK) //continue if OK
            {
                LibraryItem selectedItem = lib._items[bookIndices[bookEditForm.ItemIndex]]; //hold book selected to edit

                LibraryBook selectedBook = selectedItem as LibraryBook; //cast item as library book

                BookForm bform = new BookForm(); //new book form to load selected items to

                bform.ItemTitle = selectedBook.Title;   //add book title
                bform.ItemPublisher = selectedBook.Publisher;   //add publisher book
                bform.ItemCopyrightYear = selectedBook.CopyrightYear.ToString();    //add sting of copyright year
                bform.ItemLoanPeriod = selectedBook.LoanPeriod.ToString();  //add string of loan period
                bform.ItemCallNumber = selectedBook.CallNumber; //add call number
                bform.BookAuthor = selectedBook.Author; //add author

                result = bform.ShowDialog(); //show form with the selected book's data

                if (result == DialogResult.OK) //continue if OK
                {
                    selectedBook.Title = bform.ItemTitle;   //set new book title
                    selectedBook.Publisher = bform.ItemPublisher; //set new book publisher
                    selectedBook.CopyrightYear = int.Parse(bform.ItemCopyrightYear); //set new copyright year as int
                    selectedBook.LoanPeriod = int.Parse(bform.ItemLoanPeriod); //set new loan period as int
                    selectedBook.CallNumber = bform.ItemCallNumber; //set new call number
                    selectedBook.Author = bform.BookAuthor; //set new author
                }
            }
        }