- C# 7 and .NET Core 2.0 Blueprints
- Dirk Strauss Jas Rademeyer
- 228字
- 2025-02-25 23:58:45
Finishing up the ImportBooks code
Let's have a look at the rest of the code in the ImportBooks form. The form load just populates the storage spaces list, if any existing storage spaces have been previously saved:
private void ImportBooks_Load(object sender, EventArgs e) { PopulateStorageSpacesList(); if (dlVirtualStorageSpaces.Items.Count == 0) { dlVirtualStorageSpaces.Items.Add("<create new storage
space>"); } lblEbookCount.Text = ""; }
We now need to add the logic for changing the selected storage space. The SelectedIndexChanged() event of the dlVirtualStorageSpaces control is modified as follows:
private void dlVirtualStorageSpaces_SelectedIndexChanged(object sender, EventArgs e) { int selectedValue =
dlVirtualStorageSpaces.SelectedValue.ToString().ToInt(); if (selectedValue == (int)StorageSpaceSelection.New) // -9999 { txtNewStorageSpaceName.Visible = true; lblStorageSpaceDescription.Visible = true; txtStorageSpaceDescription.ReadOnly = false; btnSaveNewStorageSpace.Visible = true; btnCancelNewStorageSpaceSave.Visible = true; dlVirtualStorageSpaces.Enabled = false; btnAddNewStorageSpace.Enabled = false; lblEbookCount.Text = ""; } else if (selectedValue !=
(int)StorageSpaceSelection.NoSelection) { // Find the contents of the selected storage space int contentCount = (from c in spaces where c.ID == selectedValue select c).Count(); if (contentCount > 0) { StorageSpace selectedSpace = (from c in spaces where c.ID ==
selectedValue select c).First(); txtStorageSpaceDescription.Text =
selectedSpace.Description; List<Document> eBooks = (selectedSpace.BookList ==
null)
? new List<Document> { } : selectedSpace.BookList; lblEbookCount.Text = $"Storage Space contains
{eBooks.Count()} {(eBooks.Count() == 1 ? "eBook" :
"eBooks")}"; } } else { lblEbookCount.Text = ""; } }
I will not go into any detailed explanation of the code here as it is relatively obvious what it is doing.