/// <summary> /// ButtonEvent that creates a new loan if the user is in the process of creating a loan, or as returnbutton if the user has an existing loan selected. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void returnCreateLoanBTN_Click(object sender, EventArgs e) { if (createNewLoan) { if (ValidLoanInput()) { if (loanInputBookCopy.OnActiveLoan()) { string confirmBoxText = "The Bookcopy you selected is already on loan"; string confirmBoxTitle = "Bookcopy already on loan"; InfoPopup(confirmBoxText, confirmBoxTitle); } else // Allowed to create a new loan { Loan newLoan = new Loan(loanInputBookCopy, loanInputMember, timeOfLoanDTP.Value, dueDateDTP.Value); loanService.Add(newLoan); UpdateLoanInfoPanel(newLoan); // Update info panel to reflect the loan return. UpdateLoansList(currentLoanDisplay); int loanIndex = currentLoanDisplay.FindIndex(a => a == newLoan); LoansLV.Items[loanIndex].Selected = true;// Reselects the loan from the LoansListView UpdateAvailableBookCopiesAutoComplete(); } } else { string confirmBoxText = "All fields did not contain valid data. Make sure your BookCopy and Member fields contain existing data."; string confirmBoxTitle = "Invalid Loan Data."; InfoPopup(confirmBoxText, confirmBoxTitle); } } else //Return Loan { ListViewItem item = LoansLV.SelectedItems[0]; int loanID = Int32.Parse(item.SubItems[0].Text); Loan loan = loanService.Find(loanID); loanService.ReturnLoan(loan, timeOfReturnDTP.Value); if (CalculateLoanFine(loan) > 0) { string confirmBoxText = string.Format("The Book was returned {0} days late. Fine for a {0} day late return is {1}.", CalculateLoanFine(loan) / dailyFine, CalculateLoanFine(loan)); string confirmBoxTitle = "Fine for late return"; InfoPopup(confirmBoxText, confirmBoxTitle); } else { string confirmBoxText = "Loan return has successfully been registered."; string confirmBoxTitle = "Loan has been returned"; InfoPopup(confirmBoxText, confirmBoxTitle); } UpdateLoanInfoPanel(loan); // Update info panel to reflect the loan return. UpdateLoansList(currentLoanDisplay); int loanIndex = currentLoanDisplay.FindIndex(a => a == loan); LoansLV.Items[loanIndex].Selected = true;// Reselects the loan from the LoansListView } UpdateAvailableBookCopiesAutoComplete(); }
/// <summary> /// Custom draw mode to enable text coloring based on bookCopy availability. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void lbCopies_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index > -1) // If user clicked on an actual item from the listbox. { BookCopy item = lbCopies.Items[e.Index] as BookCopy; // Get the current item and cast it to MyListBoxItem e.DrawBackground(); e.DrawFocusRectangle(); if (item != null) { Color color = availableColor; if (item.OnActiveLoan()) { color = unavailableColor; } e.Graphics.DrawString( // Draw the appropriate text in the ListBox item.ToString(), // The message linked to the item lbCopies.Font, // Take the font from the listbox new SolidBrush(color), // Set the color 0, // X pixel coordinate e.Index * lbCopies.ItemHeight // Y pixel coordinate. Multiply the index by the ItemHeight defined in the listbox. ); } else { e.Graphics.DrawString( // Draw the appropriate text in the ListBox lbCopies.Items[e.Index].ToString(), // The message linked to the item lbCopies.Font, // Take the font from the listbox new SolidBrush(Color.Black), // Set the color 0, // X pixel coordinate e.Index * lbCopies.ItemHeight // Y pixel coordinate. Multiply the index by the ItemHeight defined in the listbox. ); } } }