The ImportBooks form

The ImportBooks form does exactly what the name suggests. It allows us to create Virtual Storage Spaces and to import books into those spaces. The form design is as follows:

The TreeView controls are prefixed with tv, buttons with btn, combo boxes with dl, textboxes with txt, and date time pickers with dt. When this form loads, if there are any storage spaces defined then these will be listed in the dlVirtualStorageSpaces combo box. Clicking on the Select source folder button will allow us to select a source folder to look for eBooks.

If a storage space does not exist, we can add a new Virtual Storage Space by clicking the btnAddNewStorageSpace button. This will allow us to add a name and description for the new storage space and click on the btnSaveNewStorageSpace button.

Selecting an eBook from the tvFoundBooks TreeView will populate the File details group of controls to the right of the form. You can then add additional Book details and click on the btnAddeBookToStorageSpace button to add the book to our space:

  1. You need to ensure that the following namespaces are added to your ImportBooks class:
using eBookManager.Engine; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Windows.Forms; 
using static eBookManager.Helper.ExtensionMethods; 
using static System.Math; 
  1. Next, let's start at the most logical place to begin with, which is the constructor ImportBooks() and the form variables. Add the following code above the constructor:
private string _jsonPath; 
private List<StorageSpace> spaces; 
private enum StorageSpaceSelection { New = -9999, NoSelection = -1 } 

The usefulness of the enumerator will become evident later on in code. The _jsonPath variable will contain the path to the file used to store our eBook information.

  1. Modify the constructor as follows:
public ImportBooks() 
{ 
    InitializeComponent(); 
    _jsonPath = Path.Combine(Application.StartupPath, 
"bookData.txt"); spaces = spaces.ReadFromDataStore(_jsonPath); }

The _jsonPath is initialized to the executing folder for the application and the file hard coded to bookData.txt. You could provide a settings screen if you wanted to configure these settings, but I just decided to make the application use a hard-coded setting.

  1. Next, we need to add another enumerator that defines the file extensions that we will be able to save in our application. It is here that we will see another feature of C# 7 called expression-bodied properties.