With the book information saved in the database, you will now add a second window to the project so that users can view their book catalogs offline. The Books Catalog window will be invoked by clicking on the View Catalog button that you've already added to the Library Application main window. By clicking on any title listed in the catalog, the user will be able to display its details, including an image of its cover.
You'll build the Books Catalog window using the new Data Source Configuration Wizard and two new controls: DataGridView and BindingNavigator. With the exception of the event handler for the View Catalog button, you'll accomplish this task without writing any code.
4.2.1. Create the Books Catalog Window
First you need to create the window that will display the book catalog and make it accessible from the Library Application main window.
To create the Books Catalog page, add another form to the project by right-clicking on the project name, LibraryApp, in Solution Explorer and then selecting Add New Item… Select the Windows Form template and use the default name LibraryApp project in the Solution Explorer.
Set the Text property of Form2.vb to "Books Catalog," which changes the title of the form to "Books Catalog." Now the title reflects the purpose of the form in the application.
Finally, you need to link the Books Catalog page to the main application window. On the Form1.vb design page, double-click on the View Catalog button and enter the single bold line of code in Example 4-7.
Example 4-7. btnViewCatalog Click event handler
Private Sub btnViewCatalog_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnViewCatalog.Click Form2.ShowDialog() End Sub
When a user clicks the View Catalog button, the Books Catalog page (Form2.vb) will be displayed.
4.2.2. Binding Library Data to the Books Catalog Form
You will once again use the data-binding features of VB 2005 to display all the records in the database on the newly added form.
Go to Data Show Data Sources to display the Data Sources window.
Figure 4-27. Binding the Titles table to a DataGridView control
Change the binding of the Titles table to the DataGridView control (see Figure 4-27).
Drag-and-drop the Titles table from the Data Sources window onto Form2.vb. A DataGridView and a BindingNavigator control will be created automatically. In the DataGridView Tasks menu, check the Enable Column Reordering checkbox so that users can sort the rows of their catalog entries by field. The DataGridView Tasks menu is shown in Figure 4-28.
Figure 4-28. Configuring the DataGridView control
Since the list of titles shown in the Books Catalog window doesn't have to include everything there is to know about each book, it makes sense to display the Title, Authors, and Publisher fields only. To remove the unwanted fields, go to the DataGridView Tasks menu again, click on the Edit Columns link, and remove the following fields:
The result is a list of books that displays only the title, authors, and the publisher.
You will now change the binding of the Titles table in the Data Sources window from DataGridView to Details. This will allow you to display individual records instead of multiple records on the form.
Now drag-and-drop the Titles table from the Data Sources window onto Form2.vb.
Finally, you'll want to add a Close button to the Books Catalog window so users can close the form when they've finished using it. To do that, add a Button control to Form2.vb and set its Text property to "Close." Name the Button control btnClose. Form2.vb will now look like the version shown in Figure 4-29.
Figure 4-29. Form2 with the various controls
To wire up the Click event handler for the Close button, double-click on the Close button and enter the code shown in Example 4-8. It's worth noting that this is the only code you've had to write to create this form.
Example 4-8. Close button Click event handler
Private Sub btnClose_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnClose.Click ' Me refers to the current object (Form2) Me.Close() End Sub
One last step remains. On Form2, set the PictureBox control's SizeMode property to AutoSize so that the cover of a book can be displayed in its original downloaded size.
4.2.3. Testing the Application
You can now test the application by pressing F5. On Form1.vb, click on the View Catalog button to display Form2.vb, as shown in Figure 4-30.
Figure 4-30. Displaying the catalog of books
Try editing existing records by modifying the data in the DataGridView control or the text boxes on the right of the window. To save changes to the database, you need to click the Save Data button, as shown in Figure 4-31. You can also delete records by clicking on the Delete button and then the Save Data button to effect the change.
Note that for simplicity, you will not be able to add new records in this window as the ISBN field is non-editable.
Figure 4-31. Adding and deleting records