Paul S. Cilwa

Working with Templates

When you first start Visual Basic, the New Project dialog appears. It contains a list of all the types of projects you can make. Some of these are "starter" projects (skeletons), and some are wizards that build a skeleton to order.

Some of these you are probably already familiar with; but let's just list them all for completeness' sake.

Standard EXE

This is the "bare bones" project you usually start out with, and are probably most familiar with.

ActiveX EXE

This project is used to create reusable COM objects, that can then be used in other applications—whether written in Visual Basic or not! The ActiveX EXE is not usually a stand-alone executable, although it is possible to make it run on its own. The reason to make it an EXE (instead of a DLL, see below) is so that if it crashes, it won't necessarily bring down the client application with it. The cost is slightly slower access of the reusable objects.

ActiveX DLL

As above, but the COM objects will, when instantiated, reside in the same memory space as their clients, resulting in faster access but no protection—if the COM object crashes, it will take the client with it.

ActiveX Control

Similar to an ActiveX DLL, except that the COM objects are visual—that is, they are controls that can be placed on forms, dialogs, and in documents of other applications. An undocumented limitation is that non-control objects cannot share the ActiveX control module. ActiveX control modules have a .OCX extension.

VB Application Wizard

The wizard itself is well-written, but the code it emits is far more trouble than its worth—far easier to write a new project from scratch! The wizard generates, at your request, an application in either MDI or SDI or Explorer configuration. The MDI configuration is the worst; the File..Open, File..Page Setup and Window..New Window commands are coded incorrectly, and the architecture of the project cannot be used to implement a proper MDI application. And, of course, the biggest crime is that the code is linear—old-fashioned, not at all object-oriented.

VB Application Wizard

Used to create a multi-panel "wizard" or transform application. This wizard is useful. Be aware that, by default, this project creates a .DLL; but you can change the option (via Project..Properties) to a stand-alone .EXE if you prefer.

Data Project

This option creates a project prepared to work with datasets. It includes a data connection module, and a data report module. If you don't wish to generate a report in your project, you can simply remove that module.

IIS Application

An IIS (Internet Information Server) application is a Visual Basic application that lives on a Web server and responds to requests from the browser. An IIS application uses HTML to present its user interface and uses compiled Visual Basic code to process requests and respond to events in the browser.

For an excellent article on IIS applications, including examples, click here.

Addin

This is a type of ActiveX DLL that includes components that support its use from the AddIn menu on many Microsoft applications, including Visual Basic itself. This project comes with a special Add-in designer.

ActiveX Document DLL

ActiveX documents are a blur between a traditional, static, document, and a program. ActiveX Document DLLs  can only be accessed through other, ActiveX Document clients, such as Internet Explorer or Office Binder; and they run in the same address space as the client, meaning if the document crashes, the client crashes.

ActiveX Document EXE

Like an ActiveX DLL, but runs in its own address space to provide crash-protection for its client.

DHTML Application

A DHTML application is a Visual Basic application that uses a combination of Dynamic HTML and compiled Visual Basic code in an interactive, browser-based application. A DHTML application resides on the browser machine, where it interprets and responds to actions the end user performs in the browser.

VB Enterprise Edition Controls

Same as Standard EXE, but the additional ActiveX Controls that come with the Enterprise Edition have already been made part of the project. You will need to manually remove the ones you don't use.

Form Templates

After you have created your project, you can add additional forms to it (and probably will).

Visual Basic comes with several useful (and several not-so-useful) forms that you can use as is, or as starting points for further enhancement.

One annoying thing is that most of the forms' default names use Microsoft's silly "Hungarian Notation" (the Gabor sisters should sue) naming convention, beginning with the letters "frm". Fortunately, the names are easy enough to change.

Form

A standard, blank form; this is the substrate on which all the other forms listed are based.

About Dialog

This dialog obtains its information directly from your running project, so you don't have to do much to it to add About Box capability to your project. However, the icon doesn't change automatically; and not every programmer feels the need to devote nearly as much space to warning the users as to telling them what they are running! Also, the System Info button provides a useless, and complicated, feature that you probably won't want.

VB Data Form Wizard

A nice idea, but this wizard doesn't recognize the new format of Access 2000 databases...so what the heck good is it?

Web Browser

Amazingly, this form creates a complete Web browser! It uses Internet Explorer technology, and is part of the reason IE must be present in the Windows operating system even if your preferred browser is Netscape Navigator.

When you add the form to your project, its MDIChild property is set to True; if you don't wish to use it that way, you can easily change it to False. Also, there is a Show method invocation in the form's Load event; so you may need to tweak the code for your own usage.

Dialog

Hardly worth it; this pre-designed form contains OK and Cancel buttons, but no code to make them work. Still, most dialog do have these buttons; so using this form could save you 30 seconds or so.

Login Dialog

A standard login dialog; you'll have to add the password-validating code, but other than that, it's all there for you. After showing the form modally, read its LoginSucceeded property to determine whether to proceed.

Splash Screen

This one is ridiculous. It obtains some, but not all, of its information from the running project; so you still have to make modifications to it. It has no internal timer, but includes code (that you'll have to remove) to remove the splash screen when the user clicks on it or the keyboard. That allows the user to remove it while the application is still initializing. And, speaking of lengthy initializations, there is no built-in progress bar. But, not to worry; there is a label supplied for the ever-present disclaimer.

Tip of the Day

A nifty little form that works! You supply a text file containing as many "tips" as you like, one per line. The form displays them at random. It even includes a checkbox for turning tips off, and stores the state of the checkbox in the system Registry.

You might like to make the name of the tip file a property of the form, to make changing it at runtime easier; it would also be a good idea to expose that checkbox value as a property, so that it could be reset by an option dialog in the main application.

ODBC Login

This one seems to work. I work with Access databases, mostly; so I haven't had a chance to check this thoroughly. Yet, it comes up cleanly with a list of the various data sources on my computer. I would certainly plan to use this in an ODBC project.

Options Dialog

This dialog uses the dreaded TabStrip control, which is not the easiest way to create a tabbed dialog. The TabStrip control is only the tab strip; the body of the "tabs" (the sub-forms) is an array of picture boxes, each with its own child forms, and you (the designer) can only see and work with the one on top.

I recommend you not use this stock form; create one yourself with the Microsoft Tabbed Dialog (SSTab) control. Trust me, it'll save you a ton of headaches.

Make Your Own Templates!

The template facility of Visual Basic is far more valuable than the somewhat mixed bag of templates supplied with it would suggest. That's because you aren't limited to those templates...you can supply your own!

Templates are stored in a folder nestled beneath the Visual Basic folder:

That is, assuming you have installed Visual Studio or Visual Basic into the default folders, 

c:\Program Files\Microsoft Visual Studio\Vb98\Template

Now, within the template folder, there are a series of additional folders, one for each type of object for which you might wish to have a template:

Where Do You Want Your Templates Today?

You can also place the template folders wherever you wish. A common choice would be on a network drive, where the templates could be shared by all the programmers on your team. 

Change template folders location.

Use Visual Basic's Tools..Options command, click on the "Environment" tab, and type the desired folder into the "Templates Directory" text box.

You only need specify the location of the top-level Template folder; its subfolders are assumed to be there.

A Template Project

You may have an entire project you'd like to use as a template. Or, you may have a test project you use to develop and test template folders, classes, or other components you've written.

In your project, using the Project Explorer window, right-click on each item you'd like to be a template, and choose Save As.... Locate the desired templates folder, and save the module there, giving it a descriptive name. (The name you give will show up in the dialog from which the template module is chosen.)

If you want to save the entire project, after saving the components, save the project itself (File..Save Project As...) in the Template/Projects folder.

What if the project contains modules that are not useful as stand-alone forms or classes? I recommend creating a new subfolder beneath Projects, named after the project. This subfolder will not appear in the New Projects dialog. But you can save your extra project components in this folder.

Test Case: MDI Skeleton

For example, suppose you have a project (such as we built in the Introductory course) called, "MDI Skeleton". Realizing that this project contains reusable parts, in addition to being a candidate for templatizing itself, you should examine the project and place the components where the reusable ones can, in fact, be reused.

To remind you, a list of the project components is shown on the right.

Of these components, the About form is the only one which can be used, as is, in any project. This is the combination About box and Splash screen, complete with built-in Progress Bar.

So, the first thing we do is right-click on About, and select Save About.frm as... from the context menu. When the "Save File As" dialog appears, we navigate to the Forms subfolder of the Templates folder, then choose a descriptive file name for the form, as shown below:

After doing so, if we try to add a new form to the project, our new template shows up in the Add Form dialog as if it belonged there!

If you choose to add this form to a project, the form's default name will be the one you gave it in its natal project—in this case, "About"—but you can, of course, save it if you wish.

The other components of this project do not stand on their own; they must be part of an MDI Skeleton project. I recommend saving the project components in a subfolder beneath the "Projects" folders, named after the project. Such folders will not show up in the "Add Projects" dialog.

Although the View form can't be used independently of a project based on the MDI Skeleton, it is useful to be able to add addition views to such a project. So, next you should save View.frm to the Forms folder, giving it a name like, View for MDI Skeleton project.frm.

You can easily create a new subfolder at the same time you save the first component in it. When the "Save Form As" dialog appears, navigate to the Projects folder, then click the New Folder button on the dialog toolbox:

Type the desired name in place of "New Folder" (for example, "MDI Skeleton"), click open to enter the new folder, and then enter the desired name of the component (such as "Frame" in the above example).

Save each of the remaining components in this folder.

Finally, save the project itself (via File..Save Project As...) into the Projects folder, itself. If you change the name, while saving it, to MDI Skeleton.vbp, the name will be easier to read in the New Projects dialog.

To Test...

Choose the File..NewProject command. The New Project dialog should include, as its last item, an entry labeled "MDI Skeleton". Double-click it, and an MDI Skeleton application should build itself before your eyes!

Please note, this is not the project you just saved. This is a copy of that project, suitable for further modification and specialization.

Final Step

The MDI Skeleton project is valuable as a template project, to be sure. However, there are times you'll want to open the template project, not as a copy,  but in its original form. For example, you may want to enhance a component, or add an additional one.

You can do this by opening the project specifically. Of course, that would require your navigating all the way down to the Template\Projects folder. So, as an alternative, you can create a shortcut to that project file in a more convenient place: Your desktop, for example, or as a Start Menu command.

Do it now, while you're thinking of it. We aren't done with MDI Skeleton yet, as it turns out; we have a few more features to add that will make it an even more valuable starting place for your next, paying project!