A Simple WinForm Application using csc.exe and Notepad

The thing I like least about Visual Studio is that the tool creates unnecessarily complex templates for simple programs. For example, suppose you want a very simple WinForm "Hello" application such as the one in the screenshot below. The WinForm just has a Button control and a TextBox control. When the user clicks on the button, a message is displayed in the text box. If you launch Visual Studio then do a File | New | Project, and select a C# Windows Form Application, you get eight files (AssemblyInfo.cs, Resource.resx, Resource.Designer.cs, Settings.Designer.cs, Form1.cs, Form1.Designer.cs, Form1.resx, and Program.cs). Furthermore, the internal structure of even a simple program is overly complex, with partial classes, and semi-hidden Windows Form Designer generated code. In many testing-related situations I just want a simple, single Hello.cs file but Visual Studio does not directly allow this. So, the alternative I usually take is to type the program into notepad:
using System;
using System.Windows.Forms;
public class Form1: System.Windows.Forms.Form
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox1;
  public Form1() // constructor
  private void button1_Click(object sender, EventArgs e)
    this.textBox1.Text = "Hello there!";
  static void Main()
    Application.Run(new Form1()); // Run our form
  private void InitializeComponent() // helper method to keep constructor clean
    this.SuspendLayout(); // for better performance
    // button1
    this.button1 = new System.Windows.Forms.Button();
    this.button1.Location = new System.Drawing.Point(28, 26);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.UseVisualStyleBackColor = true;
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // textBox1
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox1.Location = new System.Drawing.Point(28, 55);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(100, 20);
    this.textBox1.TabIndex = 1;
    // Form1
    this.AutoScaleDimensions = new System.Drawing.SizeF(6.0F, 13.0F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 264);
    this.Name = "Form1";
    this.Text = "Form1";
} // class Form1
After saving this single file as Hello.cs in any convenient directory, I compile the application with:
C:\SomeWhere>csc.exe /target:winexe Hello.cs
which generates a Hello.exe file. It is possible to create custom Project templates in Visual Studio, but I argue that VS should ship with a "Simple Windows Form Application" template.


This entry was posted in Software Test Automation. Bookmark the permalink.