CodeSnip:Customizing VS 2003 Code Generation Templates
page 1 of 1
Published: 22 Nov 2004
Unedited - Community Contributed
Abstract
You can save coding time by customizing the Visual Studio .NET 2003 code generation templates.
by Michelle Beall
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 9874/ 25

One of Visual Studio's strongest features is its customizability.  As a quick introduction to Visual Studio customization I will describe how to customize the built in code generation templates.  To save time coding you can customize the code generation templates to include standard comments or perhaps even to remove those "Add Code Here" comment lines.  I will focus on C# code generation templates but this technique will apply to all templates.

Where do you begin?  Using Windows Explorer navigate to the install location of Visual Studio 2003, typically found at C:\Program Files\Microsoft Visual Studio .NET 2003.  Under the program folder you will see several folders.  The one we're interested in is the VC# folder.  This is where all the C# code generation templates and wizards are located.  The two folders under "VC#" we are going to work with are "DesignerTemplates" and "VC#Wizards".

In the DesignerTemplates folder under 1033 are a few CS class files.  These files pertain to the code behind classes for some of the wizards such as the Web Application Wizard.  The Web Application Wizard generates ASPX forms and the NewWebFormCode.cs file found here is the template used for the ASPX code behind class.

Listing 1 -NewWebFormCode.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace $NAMESPACE$
{
 /// <summary>
 /// Summary description for $CLASSNAME$.
 /// </summary>
 public class $CLASSNAME$ : System.Web.UI.Page
 {
  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {    
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
 }
}


Say your coding practices require you to place a comment block at the top of every CS file.  You could then modify the NewWebFormCode.cs file as follows:

Listing 2 - Adding standard comment block

//
// Application: 
// Author: Michelle Beall
// Date: MM/DD/YYYY
// Comments: <Comments>
//
// Version History: (Add most recent change to top of list)
// Date        Name           Description
// MM/DD/YYYY  Michelle Beall Original
//

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace $NAMESPACE$
{
 /// <summary>
 /// Summary description for $CLASSNAME$.
 /// </summary>
 public class $CLASSNAME$ : System.Web.UI.Page
 {
  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {    
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
 }
}


Perhaps you even have standard region blocks.  You can add those here too.  In fact, add anything to save time and act as an aide.

The other folder of importance under the VC# folder is VC#Wizards.  Contained within are several wizard folders that cover tasks such as adding a class to adding a web.config file to your Visual Studio project.  Under each wizard folder is a Templates folder where you will again find a 1033 folder containing CS code generation template files.  You can modify these in the same way.  Just edit the listing according to your needs.

 



User Comments

Title: Remove the A   
Name: Scott Cate
Date: 2004-11-28 11:53:20 AM
Comment:
\
\
\
\






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


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-11-22 1:10:49 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search