Start off by renaming Form1.cs
to eBookManager.cs
. This is the start-up form for the application and it will list all the existing storage spaces previously saved:
Design your eBookManager
form as follows:
When you have added the controls, your eBook Manager
form will look as follows:
Looking at the code we used earlier, you need to ensure that the following using
statements are imported:
using eBookManager.Engine; using eBookManager.Helper; using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; using System.Linq; using System.Diagnostics;
The constructor is quite similar to the ImportBooks.cs
form's constructor. It reads any available storage spaces and populates the storage spaces list view control with the previously saved storage spaces:
private string _jsonPath; private List<StorageSpace> spaces; public eBookManager() { InitializeComponent(); _jsonPath = Path.Combine(Application.StartupPath, "bookData.txt"); spaces = spaces.ReadFromDataStore(_jsonPath); } private void Form1_Load(object sender, EventArgs e) { PopulateStorageSpaceList(); } private void PopulateStorageSpaceList() { lstStorageSpaces.Clear(); if (!(spaces == null)) { foreach (StorageSpace space in spaces) { ListViewItem lvItem = new ListViewItem(space.Name, 0); lvItem.Tag = space.BookList; lvItem.Name = space.ID.ToString(); lstStorageSpaces.Items.Add(lvItem); } } }
If the user clicks on a storage space, we need to be able to read the books contained in that selected space:
private void lstStorageSpaces_MouseClick(object sender, MouseEventArgs e) { ListViewItem selectedStorageSpace = lstStorageSpaces.SelectedItems[0]; int spaceID = selectedStorageSpace.Name.ToInt(); txtStorageSpaceDescription.Text = (from d in spaces where d.ID == spaceID select d.Description).First(); List<Document> ebookList = (List<Document>)selectedStorageSpace.Tag; PopulateContainedEbooks(ebookList); }
We now need to create the method that will populate the lstBooks
list view with the books contained in the selected storage space:
private void PopulateContainedEbooks(List<Document> ebookList) { lstBooks.Clear(); ClearSelectedBook(); if (ebookList != null) { foreach (Document eBook in ebookList) { ListViewItem book = new ListViewItem(eBook.Title, 1); book.Tag = eBook; lstBooks.Items.Add(book); } } else { ListViewItem book = new ListViewItem("This storage space contains no eBooks", 2); book.Tag = ""; lstBooks.Items.Add(book); } }
You will notice that each ListViewItem
is populated with the title of the eBook and the index of an image in an ImageList
control that I added to the form. To find the images in the GitHub repository, browse to the following path:
Looking at the Images Collection Editor
, you will see that I have added them as follows:
We also need to clear the selected book's details when the selected storage space is changed. I have created two group controls around the file and book details. This code just loops through all the child controls, and if the child control is a textbox, it clears it:
private void ClearSelectedBook() { foreach (Control ctrl in gbBookDetails.Controls) { if (ctrl is TextBox) ctrl.Text = ""; } foreach (Control ctrl in gbFileDetails.Controls) { if (ctrl is TextBox) ctrl.Text = ""; } dtLastAccessed.Value = DateTime.Now; dtCreated.Value = DateTime.Now; dtDatePublished.Value = DateTime.Now; }
The MenuStrip
that was added to the form has a click event on the ImportEBooks
menu item. It simply opens up the ImportBooks
form:
private void mnuImportEbooks_Click(object sender, EventArgs e) { ImportBooks import = new ImportBooks(); import.ShowDialog(); spaces = spaces.ReadFromDataStore(_jsonPath); PopulateStorageSpaceList(); }
The following method wraps up the logic to select a specific eBook and populate the file and eBook details on the eBookManager
form:
private void lstBooks_MouseClick(object sender, MouseEventArgs e) { ListViewItem selectedBook = lstBooks.SelectedItems[0]; if (!String.IsNullOrEmpty(selectedBook.Tag.ToString())) { Document ebook = (Document)selectedBook.Tag; txtFileName.Text = ebook.FileName; txtExtension.Text = ebook.Extension; dtLastAccessed.Value = ebook.LastAccessed; dtCreated.Value = ebook.Created; txtFilePath.Text = ebook.FilePath; txtFileSize.Text = ebook.FileSize; txtTitle.Text = ebook.Title; txtAuthor.Text = ebook.Author; txtPublisher.Text = ebook.Publisher; txtPrice.Text = ebook.Price; txtISBN.Text = ebook.ISBN; dtDatePublished.Value = ebook.PublishDate; txtCategory.Text = ebook.Category; } }
Lastly, when the book selected is the one you wish to read, click on the Read eBook
button to open the file location of the selected eBook:
private void btnReadEbook_Click(object sender, EventArgs e) { string filePath = txtFilePath.Text; FileInfo fi = new FileInfo(filePath); if (fi.Exists) { Process.Start(Path.GetDirectoryName(filePath)); } }
This completes the code logic contained in the eBookManager
application.
Note
You can further modify the code to open the required application for the selected eBook instead of just the file location. In other words, if you click on a PDF document, the application can launch a PDF reader with the document loaded. Lastly, note that the classification has not been implemented in this version of the application.
It is time to fire up the application and test it out.