How to use T4 template in a Window Form Application

BenTam-3003 686 Reputation points
2022-07-25T05:55:31.067+00:00

Dear All,

How to use the "T4 template" in a Window Form Application? An example is appreciated.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,852 questions
{count} vote

Accepted answer
  1. Reza Aghaei 4,946 Reputation points MVP
    2022-07-25T11:39:51.03+00:00

    There are two types of T4 templates, Run-time and Design-time:

    • Run-time templates help defining templates which you want to run at run-time in your application to produce text output. For example, generating an email, an HTML report page, etc. So they are usually used for run-time text generation. These templates use TextTemplatingFilePreprocessor as their custom tool. More info: Run-Time Text Generation with T4 Text Templates
    • Design-time templates help defining source code and other resources of your application. For example to generate model classes based on metadata or database, or generate resources, etc. So they are usually used for design-time code generation. These templates use TextTemplatingFileGenerator as their custom tool. More info: Design-Time Code Generation by using T4 Text Templates

    Before choosing a template type, you should answer the following questions:

    1. Do you want to to generate text-output at runtime, for example to send an email, to show a message to the user, and etc.? If the answer is yes, you need to use Runtime Text templates.
    2. Do you want to generate a resource at design time as part of the Project, for example generate a C# class, a ResX resource, or etc. which will be built as part of the project? If the answer is yes, then you need Design-time Text Templates.

    Example 1 - WinForms Run-time T4

    Follow these steps:

    1. Create a new WinForms project with the following name: WindowsApplication1
    2. Add a new item and select Runtime Text template with the following name: RuntimeTextTemplate1
    3. Add the following content to the file:
          <#@ template language="C#" #>  
          <#@ assembly name="System.Core" #>  
          <#@ import namespace="System.Linq" #>  
          <#@ import namespace="System.Text" #>  
          <#@ import namespace="System.Collections.Generic" #>  
          Items:  
          <# for (int i = 1; i <= 10; i++){ #>  
          Item <#= i #>  
          <# } #>
      
    4. Save the file and if VS asked a question, let the code generator run the code and tell it not to ask the question again.
    5. Drop a button with button1 as name, on Form1 and double click on it to generate the click event handler and change the code like this:
          private void button1_Click(object sender, EventArgs e)  
          {  
              var template = new RuntimeTextTemplate1();  
              var content = template.TransformText();  
              MessageBox.Show(content);  
          }
      
    6. Run the application, click on the button and see the generated text as run-time.

    Example 2 - WinForms Design-time T4

    Follow these steps:

    1. Create a new WinForms project with the following name: WindowsApplication1
    2. Add a new item and select Text Template with the following name: TextTemplate1
    3. Add the following content to the file:
          <#@ template debug="false" hostspecific="false" language="C#" #>  
          <#@ assembly name="System.Core" #>  
          <#@ import namespace="System.Linq" #>  
          <#@ import namespace="System.Text" #>  
          <#@ import namespace="System.Collections.Generic" #>  
          <#@ output extension=".cs" #>  
          using System;  
          using System.Windows.Forms;  
          <#  
          var entities = new string[]{"Product", "Customer"};  
          foreach (var entity in entities)  
          {#>  
          public class <#=entity#>Form: Form  
          {  
              public <#=entity#>Form(): base()  
              {  
                  this.Text = "<#=entity#>";  
              }  
          }  
          <#}#>
      
    4. Save the file and if VS asked a question, let the code generator run the code and tell it not to ask the question again.
    5. Drop a button with button1 as name, on Form1 and double click on it to generate the click event handler and change the code like this:
          private void button1_Click(object sender, EventArgs e)  
          {  
              var p = new ProductForm();  
              var c = new CustomerForm();  
              p.Show();  
              c.Show();  
          }
      
    6. Run the application, click on the button and see the generated text as run-time.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.