官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 呼和浩特市| 白水县| 盈江县| 宁南县| 汶上县| 张家界市| 平安县| 阿尔山市| 康平县| 章丘市| 乌海市| 房产| 朝阳区| 吉木萨尔县| 东阳市| 义马市| 阳新县| 宁波市| 肥乡县| 阳山县| 巩义市| 牡丹江市| 永和县| 南川市| 德惠市| 康保县| 周至县| 诸暨市| 金塔县| 遂溪县| 康乐县| 余姚市| 上犹县| 阳谷县| 金川县| 波密县| 深水埗区| 河东区| 凉山| 阿拉善左旗| 绥中县|