Speed Up with Project Templates Using Visual Studio 2005 - Part 3
page 5 of 10
by Ameet Phadnis
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 38259/ 91

Creating your IWizard Implementation Class

In order to create your own IWizard class, you need to follow these steps:

·         Create a Project of the type Class Library.

·         By Default, the project will open a class file with the name Class1.  Change the class name appropriately.

·         You will need to import the following:

Listing 4

Imports System
Imports system.collections.Generic
Imports Microsoft.VisualStudio.TemplateWizard
Imports System.Windows.Forms

·         Make sure your class implements IWizard interface.

·         As soon as you implement the IWizard interface, VS will add the methods that can be implemented in this class.

With the above steps you just created your own Wizard implementation class.  In our example we are going to use a Window form to get user input regarding Server, database, userID and password information.  In order to create the window you will need to follow these steps.

·         Right Click on the project and click on Add->Windows Form. With VS 2005 you can also add dialog form directly by Add->New Item and selecting Dialog template if you are interested.

·         The form will be added.  Add four label controls and set the text to Server:, Database:, UserID: and Password:.  Add four textboxes and set the ID property to txtServer, txtDatabase, txtUserID and txtPassword.  Your form should look like the following figure.

Figure 2

·         View Code for this form.

·         Declare a private variable for the LocalHostString to store the connection String.

Private LocalHostString As String

·         Code the Ok buttons click event as follows.

Listing 5

Private Sub btnOk_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnOk.Click
        If txtServer.Text <> "" AndAlso txtDatabase.Text <> "" Then
            Shell( _
 "C:\Windows\system32\cmd.exe /C C:\WINDOWS\Microsoft.Net\Framework\v2.0.50727\aspnet_regsql.exe -S""" & _
  txtServer.Text & """ -E -d""" & _
  txtDatabase.Text & """ -A all", AppWinStyle.Hide)
            LocalHostString = "Initial Catalog=" & txtDatabase.Text & ";Data Source=" &
 txtServer.Text & ";User ID=" & txtUserId.Text &
 ";Password=" & txtPassword.Text
        End If
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
    End Sub

·         In the above code we first do the registration using the Shell command to call the ASPNet_regsql command and pass in the server and database name.  Once that is done, we build the LocalHostString based on the user input.

·         Now we need to create another function that will return the LocalHostString.

Listing 6

Public Function GetLocalHostString() As String
        Return LocalHostString
    End Function

·         Now our form is ready to accept user input for server, database, userID and password.  The next step is to call this form through Wizard implementation class.

·         In order to call this form, we will be implementing the RunStarted method in our class.

·         We need to declare LocalHostString as Private variable on our Implementation class too.  Also, we will define the form as a private member.

Listing 7

Private dbform As ArticleForm
    Private LocalHostString As String

·         This method will open the form, get the LocalHostString value and then use the System.Collections.Generic.Dictionary to replace the $LocalHostString$ variable in our template.  The code in RunStarted will look like Listing 8.

Listing 8

Public Sub RunStarted(ByVal automationObject As ObjectByVal replacementsDictionary As System.Collections.Generic.Dictionary(Of StringString), _
 ByVal runKind As Microsoft.VisualStudio.TemplateWizard.WizardRunKind, ByVal customParams() As Object) _
Implements Microsoft.VisualStudio.TemplateWizard.IWizard.RunStarted
        dbform = New ArticleForm
        LocalHostString = dbform.GetLocalHostString
        replacementsDictionary.Add("$LocalHostString$", LocalHostString)
End Sub

·         In the above code we are opening the ArticleForm (my form) as a dialog box and once the dialog box is closed we are getting the LocalHostString value.  We then add this variable to the dictionary to be used in our template at a later stage.

·         Once this is created, you need to sign this assembly and then install it in GAC to be used with your template.

View Entire Article

User Comments

Title: Clarification   
Name: Jon
Date: 2007-01-09 3:34:33 PM
As a clarification, VS will still use the old version even after I go into the cache through the control panel and remove it.
Title: Development Process   
Name: Jon
Date: 2007-01-09 3:32:22 PM
What's the best way to actually build and test a wizard from the ground up?

I have my build process for the Wizard's project set up to sign the DLL and automatically add itself to the GAC (using: "gacutil.exe /if $(TargetPath)").

The problem is they will stay in the cache after every rebuild, so I need to either update the version number in the vsTemplate file and the project after each compile, or restart Visual Studio each time I want to test a new build.

Title: VB Bug ?   
Name: frisla
Date: 2006-09-20 9:52:51 AM
Hi A

The test for C# and VB.NET wore created as separated projects - C# Custom wizard and template project, VB Custom wizard and template project.

In VB as soon as I try to access my IWizard implementation, my VB template project wil not unfold, hence the error message. The template itself executes perfectly, but again as soon as I try to reference my custom wizard, using an "WizardExtension" entry in the xx.vstemplate I get the error message.

Any suggestions

- Thanks -
Title: No   
Name: Ameet
Date: 2006-09-19 7:37:26 PM
The article was written using VB.NET and not C#.NET.

Check whether you have created the template itself for C#.NET...
Title: VB Bug ?   
Name: frisla
Date: 2006-09-19 4:01:54 PM
Why is it you get a, "Unable to copy the file 'Application.myapp' from the project template to the project. Cannot find file ...." failure when you use VB.NET (2005), but using C# is a walk in the park ??????

Is the execution of the IWizard interface different in VB.NET ?????

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-02-22 6:19:25 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search