Stephen Rakonza

Subscribe to Stephen Rakonza: eMailAlertsEmail Alerts
Get Stephen Rakonza: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Article

.NET Feature — Creating Templates for Visual Studio 2005

Easy authoring, single file distribution, and file copy installation

The replacement parameter's value will be set to an empty string in the IWizard.RunStarted method if the user chooses not to include the About Box. The following code contains code that accomplishes this. This will effectively remove the optional menu item handler code if the user chooses not to use the About Box. And in this way, the wizard extension component is no longer specific to C#. It can be used in templates created for any language.

if (!this.hasAboutBox)
{
     replacementsDictionary["$AboutMenuItemHandler$"] = "";
}

Another interesting use of the wizard extension component is to invoke the Visual Studio 2005 automation object model, or the DTE object. The automation object model can be used to control the behavior of various components inside Visual Studio 2005. To illustrate its usage, we will use the DTE object to highlight a code segment when the project template finishes generating the project.

It is a common practice for the generated project to contain instructions about how to further develop the project. It will be nice to have the wizard extension component highlight the instruction when the template finishes generating. In the MDI application project template, we would like to highlight a portion of the code contained in ParentForm.cs. To accomplish this, first we need to make sure the file ParentForm.cs has the OpenInEditor attribute set to true in the .vstemplate file so that it is opened in the editor after the project template finishes. Also, to make it open in the code editor instead of the default Windows Form designer, its SubType attribute is set to Code in the project file MDIApplication.csproj. In this way, when the template finishes generating, the generated ParentForm.cs file will be opened in the code editor.

The next step is to get hold of the DTE object from inside the wizard extension component so that we can invoke it later to highlight the desired code. For a project template, this can be done from the IWizard.ProjectFinishedGenerating method:

void IWizard.ProjectFinishedGenerating(EnvDTE.Project project)
{
     this.dteObject = project.DTE;
}

Finally, the DTE object is invoked when the wizard finishes executing. The sample code used to invoke the DTE object to highlight the desired code in the code editor. In this sample, the comment Modify is introduced to the code to add a different type of MDI child window highlighted.

void IWizard.RunFinished()
{
   string srchPattern1 = "Modify the following code to add a different type of MDI child window.";
   TextSelection objSel = (TextSelection)this.dteObject.ActiveDocument.Selection;
   TextRanges textRanges = null;

   // Move to the beginning of the document
   objSel.StartOfDocument(false);

   // Select desired line
   objSel.FindPattern(srchPattern1, 0, ref textRanges);
}

This completes the coding of the wizard extension component. The entire source code for the wizard extension component can be downloaded from this article's link under http://www.sys-con.com. In order for Visual Studio 2005 to invoke this wizard extension component, it needs to be installed to the GAC and added to the .vstemplate file's WizardExtension element. After these steps, zip the project template files, copy it to the project template folder and it will be ready for use by Visual Studio 2005.

<WizardExtension>
   <Assembly>MDITemplateWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=a81d8e0fa67c91f2,
ProcessorArchitecture=MSIL</Assembly>
   <FullClassName>MDITemplateWizard.ProjectTemplateWizard</FullClassName>
</WizardExtension>

In this section, the project template is used to illustrate the wizard extension component technique. The wizard extension component is not limited to the project templates. The same technique can be applied to item templates as well. Please refer to MSDN if you want to learn more about the wizard extension component.

Distribute the Template
If the template contains only the .zip file, it can be distributed through file copy. If it contains an additional wizard extension component as our sample does, it can be distributed through a simple batch file, a custom installer or the Visual Studio 2005 community tool Content Installer. Please refer to MSDN for more details.

Conclusion
In this article, we introduced how to create project templates and item templates for Visual Studio 2005. Hopefully you can utilize the templates to simplify the development process for you and your team.

More Stories By Xin Yan

Xin Yan has been a software design engineer at Microsoft for over 7 years. He works on Visual Studio developer tools platform team.

More Stories By Stephen Rakonza

Stephen Rakonza has been a software design engineer at Microsoft for over 9 years. He works on Visual Studio developer tools platform team.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.