Create Project Item Wizardsby Niel Bornstein, author of .NET and XML
In a recent project, my team had to produce a platform that included a set of base classes that other developers could use to develop Windows forms in Visual Studio .NET 2003. As we worked on the base classes, it became clear that our developers would be able to crank out their forms a lot faster if we could also generate stubs and skeleton code for all of the methods they needed to override in their own classes.
It's not a unique problem, and in fact it's one that Microsoft has already solved for the most basic file types. Every time you right-click on a project in Visual Studio .NET and select Add New Item..., the IDE creates a file and, depending on the type of file, even inserts the class name and other standard formatting.
We needed to go a step further and gather some information from the developer, such as the names of other related classes, whether to include certain components on a generated form, and whether to inherit the new form from one base class or another. One way to gather this kind of information from the user is the wizard metaphor.
What are Wizards?
A wizard is a tool that guides you through a particular process. The first wizards were introduced as a feature of Microsoft Publisher in 1991. Since then, they have appeared throughout the Microsoft product line, but they have gotten perhaps their best use in Microsoft's operating systems, where they can be seen every time you set up networking, download photographs from a digital camera, or send a fax.
As a tool included with an operating system or software package, a wizard can save an end user from a potentially frustrating experience. Software used to come with manuals, and the functionality provided by wizards would be explained in the step-by-step instructions user manuals used to provide. Those instructions were not adaptive, like some wizards, however, and required the user to go back and forth between the manual and the software product being used. By integrating the task being accomplished with the step-by-step instructions that allow the user to accomplish the task, and adding the ability to adapt the process to other conditions, wizards have been a great boon to software usability.
In development tools, however, wizards can have the effect of hiding complexity from developers. Some hard-nosed, old-school programmers -- and I'll have to admit that I am one of them -- would argue that hiding complexity can be counter-productive in software development. If a black -- or gray -- box does all of the work for you, you may never understand what is really being done behind the scenes. A developer who doesn't understand what his program is doing is arguably a poor developer.
On the other hand, developers come in different flavors, with varying skill levels, and wizards can be created in such a way that they hide the complexity of a process without obscuring the process' products. If all you need, for example, is a kind of cut-and-paste on steroids, a wizard can definitely save you time. Such was the case in my project, and so I entered into the dark and murky world of Visual Studio .NET wizards.
How Is It Done?
My first stop on the road to wizardry was the MSDN topic "Creating Add-Ins and Wizards." While there is plenty of information available there about the Visual Studio .NET automation model, you have to drill down a bit to get to the information on creating wizards. The page titled "Creating a Wizard" seems a likely place to start.
A little further reading indicates that you need to create a DLL and implement the
IDTWizard has a single method,
Execute, which allows you to present the wizard interface to the user. Implementing the
Execute method leaves the presentation details completely up to you.
This seems an inefficient use of time for someone who really just wants to throw a simple one- or two-page wizard up on the screen. Rather than reinvent the wheel, it's much easier way to copy an existing wizard and change it to match your needs. To that end, let's look at some wizards that get installed with Visual Studio .NET by default.
Shortcuts to Your First Wizard
Most of the time, when you select the Add New Item... command from a project's context menu, you are presented with a list of file types and given the opportunity to type a file (and class) name. Visual Studio .NET then happily creates a file of the correct type, seemingly generating skeleton code for the named class, stubbing out any properties and methods required by the parent class, and adds it to the project. The familiar dialog box is shown in Figure 1.
Figure 1. The Add New Item dialog box.
I say "seemingly," because the code is not all generated on the fly. Each of the item types listed in the Add New Item dialog box has a set of scripts and templates behind it, which contain the logic needed to create the new file. In some cases, such as when creating a new class, the script does nothing and the template only inserts the class declaration and stubs out the constructor. In other cases, such as when creating an inherited control, the template may do more.
A little snooping about in the Program Files\Microsoft Visual Studio .NET 2003 directories on your PC will turn up the VC# subdirectory, where all the C# wizards and add-ins live. There's a directory under that one that contains wizards, called VC#Wizards. To pick one more or less at random, let's look at
There are several components to any Visual Studio .NET wizard, as you can see in
CSharpClassWiz. They are HTML files, images, scripts, and templates.
Pages: 1, 2