n the days of ASP.NET 1.x, Microsoft first created ASP.NET Starter Kits to help developers jumpstart Web development. ASP.NET Starter Kits are sample ASP.NET applications that provide code as an extensible template for accomplishing common Web development tasks. Each Starter Kit is complete and well documented so that users can become productive right away.
For example, one Starter Kit that Microsoft created, the Personal Web Site Starter Kit, provides the basic framework on how to build an ASP.NET Web site that contains an online photo gallery, login facility for secure access, as well as a discussion forum. The Starter Kit will contain the base functionality and the developer will customize and enhance it to suit his purpose. This greatly increases the developer’s productivity and provides a good way to learn through extending the original application. And in Visual Studio 2005, Microsoft has extended the Starter Kits to include Windows applications.
For a list of Starter Kits provided by Microsoft, check out the following URLs:
But Microsoft isn’t the only one who can make a Starter Kit. In this article, I will show you how you can build a Starter Kit in Visual Studio 2005 and share it with the community. The application I will build is a personal library Windows application that catalogs detailed information about the books you own. The application identifies books you own using their ISBN numbers; this way, the application can use the Internet to retrieve data about that book and you don’t need to type metadata for each book manually. You can add a book to the application by using a barcode scanner, if you have access to one, or you can enter the ISBN numbers manually. The application will then download the detailed book information (including the book cover) from Amazon’s Web service. You can also search for books from Amazon and then add books to the local database.
This application will demonstrate the following techniques/technologies in Windows Forms 2.0:
- Drag and drop data-binding
- Data access
- Changing the cursor
- Multithreading using the new BackgroundWorker control
- Accessing web services
|Author’s Note: The sample application is adapted from the case study in my latest book, “Visual Basic 2005 Jumpstart” (published by O’Reilly). For detailed information on how to build the sample application, check out Chapter 4: Developing a Windows Application.|
Creating the Application
The first step in creating the Starter Kit is to create the Windows application. Using Visual Studio 2005, create a new Windows application using Visual Basic and name the project LibraryApp. In the default Form1, I have populated the form with the necessary controls (see Figure 1).
|Author’s Note: I will not be showing you the detailed steps to populate the form. For this, I suggest you download the source code that accompanies this article. Similarly, the coding of the application can be found from the source code.|
I have also added a second form to the application and populated the controls as shown in Figure 2. This window will display the list of titles added to the local database. Clicking on the Close button will close the form and return to the first form.
In addition, you will need a database to hold the detailed book information. Add a SQL Server 2005 Express edition database to the project and name it Library.mdf. The schema for this database is shown in Figure 3.
Finally, to consume information from Amazon, you’ll need to add a Web Reference to the Amazon web service (see Figure 4). To add a web service, right-click on the project name in Solution Explorer and select Add Web References. You then specify the URL of the Amazon.com web service.
This is how the application will work: You can enter the ISBN of a book in the Search Keywords textbox and click the Get Info button (see Figure 5). The Search Results listbox will show the list of matching titles. To view the detailed book information, click the titles in the listbox and the detailed information will be displayed on the right pane of the window. To add the title to the local database, click the Add Title button.
|Figure 5. Book Search: The screen shot shows how a user would use this application to search for books by keyword.|
My application uses the BackgroundWorker control (new in Windows Forms 2.0) to perform multithreading so as to ensure that the user interface does not freeze when it is accessing Amazon’s web service. It will also display the hourglass icon to indicate that it is busy while waiting for the results to be returned from Amazon.
This application is optimized for use with a barcode scanner. Typically a barcode scanner functions much like a keyboard?it scans in a barcode and pass in the string of digits (and appends a carriage return) just as if you had typed it using a keyboard. So if you are using a barcode scanner set the focus on the Search Keywords textbox and scan the barcode using the scanner. My application will automatically detect the carriage return character returned by the scanner and trigger the Get Info button.
Besides searching by ISBN numbers, you can also search by keywords, as shown in Figure 6.
To view the catalog of books that you have added into the database, click the View Catalog button. Form2 will display the list of books using the DataGridView control (see Figure 7). You can also navigate the records using the BindingNavigator control (located at the top of the form).
Adding the Documentation
|Figure 8. Documentation: Documentation.txt, a text file, is being added to the Starter Kit. This file will stay with your Starter Kit wherever it goes.|
Once the application is built and working correctly, you should add some documentation to the project so that developers using your Starter Kit can understand how the application works. For example, you might provide the link for the developer to register for a free Amazon web service account and how to obtain the subscription ID for accessing the web service.
You can add an HTML document or just a plain text file to the project. For simplicity, add a Text file to the project and name it Documentation.txt (see Figure 8).
Exporting the Template
With the application and documentation completed, you are now ready to export the project. In order to make your application sharable, you have to put it into a template. You can do so via the File–>Export Template menu item.
You will be asked to choose a template type. For creating Starter Kits, choose the Project template. The item template is used for something less than a full project, such as a Form, Class, Module, etc. This solution has only one project, so simply choose the LibraryApp project (see Figure 9). Click Next.
In the next dialog box, you can assign your own icon to represent the template (see Figure 10). Name your template LibraryApp Starter Kit. Uncheck the “Automatically import the template into Visual Studio” option. While this is a handy option, it’s useful to learn how to manually create a Starter Kit. Click Finish.
That’s it! Your template will be created in the following folder:
C:Documents and SettingsWei-Meng LeeMy DocumentsVisual Studio 2005My Exported Templates
Place the exported template files into a .zip and name it Library App Starter Kit.zip. You will use this .zip file in the next section.
Open Specific Files When Launching Templates
Recall that the template you’ve just created has a documentation file. You want this file to be displayed every time someone creates a project using your template. To do so, you need to modify the MyTemplate.vstemplate configuration file located within the LibraryApp Starter Kit.zip file.
First, extract the content of LibraryApp Starter Kit.zip into the default folder?LibraryApp Starter Kit. Then go into the LibraryApp Starter Kit folder and use Notepad to edit the MyTemplate.vstemplate file.
In particular, insert the following line (shown in bold):
LibraryApp Starter Kit VisualBasic 1000 true LibraryApp Starter Kit true Enabled true __TemplateIcon.ico app.config Documentation.txt
Once the MyTemplate.vstemplate file is modified, you need to re-zip everything in the folder back into the LibraryApp Starter Kit.zip file again. You can select all the files in the folder, right-click and then select Send To–>Compressed (zipped) Folder (see Figure 11).
A .zip file containing all the files in the folder will appear at the bottom of the window. Rename this .zip file LibraryApp Starter Kit.zip and move it out to your My Documents area in a folder called Visual Studio 2005My Exported Templates.
Packaging the Starter Kit using the Visual Studio Installer
Now that the template is created and modified, you need one more step to package it into a Starter Kit. In the folder you just created, create a new file and save it as LibraryApp.vscontent. Populate the LibraryApp.vscontent file with the following:
LibraryApp Starter Kit.zip Library Application Starter Kit VSTemplate for VB project VSTemplate 1.0
In the same folder, create a new empty .zip file (File–>New–>Compressed (zipped) Folder). Drag and drop the LibraryApp Starter Kit.zip and LibraryApp.vscontent files into the newly created .zip file. Rename the .zip file LibraryApp Starter Kit.vsi (see Figure 12).
That’s it! The .vsi file is now a Starter Kit.
Installing the Starter Kit
Once the Starter Kit is created, you can distribute it to your fellow developers. A good way to share is to put it on the web for download.
To install the Starter Kit, simply double-click on the .vsi file and the installer will display the dialog shown in Figure 13. Click Next to proceed. You will be warned about security risks of the content you are going to install. Click Yes to proceed. In the final dialog, Click Finish and then Close.
To verify that the Starter Kit is installed properly, start Visual Studio 2005 and create a new project (File–>New–>Project). You will see the LibraryApp Starter Kit template under the My Templates section (see Figure 14). Select the template and click OK. You will also find that the documentation file is automatically loaded.
You can press F5 to verify that the application works as stated (remember to use your own Amazon subscription ID). You can now proceed to enhance the application to suit your own needs.
In this article, you have seen the steps to create a Starter Kit. Starter Kit is a good way to learn programming and provides a stepping stone to learning best practices from the experts in the industry.