- C# 7 and .NET Core 2.0 Blueprints
- Dirk Strauss Jas Rademeyer
- 401字
- 2021-08-27 19:55:24
Saving a selected book to a storage space
The following code basically updates the book list in the selected storage space if it already contains the specific book (after confirming with the user). Otherwise, it will add the book to the book list as a new book:
private void UpdateStorageSpaceBooks(int storageSpaceId) { try { int iCount = (from s in spaces where s.ID == storageSpaceId select s).Count(); if (iCount > 0) // The space will always exist { // Update StorageSpace existingSpace = (from s in spaces where s.ID == storageSpaceId select s).First(); List<Document> ebooks = existingSpace.BookList; int iBooksExist = (ebooks != null) ? (from b in ebooks where $"{b.FileName}".Equals($"
{txtFileName.Text.Trim()}") select b).Count() : 0; if (iBooksExist > 0) { // Update existing book DialogResult dlgResult = MessageBox.Show($"A book
with the same name has been found in Storage Space
{existingSpace.Name}.
Do you want to replace the existing book
entry with this one?",
"Duplicate Title", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2); if (dlgResult == DialogResult.Yes) { Document existingBook = (from b in ebooks where $"
{b.FileName}".Equals($"
{txtFileName.Text.Trim()}") select b).First(); existingBook.FileName = txtFileName.Text; existingBook.Extension = txtExtension.Text; existingBook.LastAccessed =
dtLastAccessed.Value; existingBook.Created = dtCreated.Value; existingBook.FilePath = txtFilePath.Text; existingBook.FileSize = txtFileSize.Text; existingBook.Title = txtTitle.Text; existingBook.Author = txtAuthor.Text; existingBook.Publisher = txtPublisher.Text; existingBook.Price = txtPrice.Text; existingBook.ISBN = txtISBN.Text; existingBook.PublishDate =
dtDatePublished.Value; existingBook.Category = txtCategory.Text; } } else { // Insert new book Document newBook = new Document(); newBook.FileName = txtFileName.Text; newBook.Extension = txtExtension.Text; newBook.LastAccessed = dtLastAccessed.Value; newBook.Created = dtCreated.Value; newBook.FilePath = txtFilePath.Text; newBook.FileSize = txtFileSize.Text; newBook.Title = txtTitle.Text; newBook.Author = txtAuthor.Text; newBook.Publisher = txtPublisher.Text; newBook.Price = txtPrice.Text; newBook.ISBN = txtISBN.Text; newBook.PublishDate = dtDatePublished.Value; newBook.Category = txtCategory.Text; if (ebooks == null) ebooks = new List<Document>(); ebooks.Add(newBook); existingSpace.BookList = ebooks; } } spaces.WriteToDataStore(_jsonPath); PopulateStorageSpacesList(); MessageBox.Show("Book added"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
Lastly, as a matter of housekeeping, the ImportBooks form contains the following code for displaying and enabling controls based on the button click events of btnCancelNewStorageSpace and btnAddNewStorageSpace buttons:
private void btnCancelNewStorageSpaceSave_Click(object sender, EventArgs e) { txtNewStorageSpaceName.Clear(); txtNewStorageSpaceName.Visible = false; lblStorageSpaceDescription.Visible = false; txtStorageSpaceDescription.ReadOnly = true; txtStorageSpaceDescription.Clear(); btnSaveNewStorageSpace.Visible = false; btnCancelNewStorageSpaceSave.Visible = false; dlVirtualStorageSpaces.Enabled = true; btnAddNewStorageSpace.Enabled = true; } private void btnAddNewStorageSpace_Click(object sender, EventArgs e) { txtNewStorageSpaceName.Visible = true; lblStorageSpaceDescription.Visible = true; txtStorageSpaceDescription.ReadOnly = false; btnSaveNewStorageSpace.Visible = true; btnCancelNewStorageSpaceSave.Visible = true; dlVirtualStorageSpaces.Enabled = false; btnAddNewStorageSpace.Enabled = false; }
All that remains now is for us to complete the code in the Form1.cs form, which is the start-up form.
推薦閱讀
- Python進(jìn)階編程:編寫更高效、優(yōu)雅的Python代碼
- 新編Premiere Pro CC從入門到精通
- Asynchronous Android Programming(Second Edition)
- NGINX Cookbook
- Getting Started with React Native
- Mastering Backbone.js
- Learning Ionic
- 小程序從0到1:微信全棧工程師一本通
- PHP+MySQL動(dòng)態(tài)網(wǎng)站開發(fā)從入門到精通(視頻教學(xué)版)
- Groovy 2 Cookbook
- 數(shù)據(jù)結(jié)構(gòu)與算法詳解
- PHP程序設(shè)計(jì)高級(jí)教程
- Puppet Cookbook(Third Edition)
- HTML5 and CSS3:Building Responsive Websites
- Mastering Swift 4(Fourth Edition)