A New Windows Forms Application
In order to create an application that runs on Windows, choose File – New – Project. This opens up the New Project window, as shown in Figure 1.
Figure 1. New Project Window for creating Windows Forms Application
As shown in Figure 1, you can create a Smart Client application by choosing the Windows Forms Application template and providing the necessary details like name and location.
If you choose a different .NET Framework version at the top right of the New Project window, then only the templates available for that version will be shown. For example, if you choose .NET Framework version 2.0, then you will not see the WPF related templates (WPF Application, WPF User Control Library, WPF Browser Application, and WPF Custom Control Library). The WPF technology is available in .NET Framework version 3.0 and later.
After you click OK in the New Project dialog box above, Visual Studio will generate a Solution, a Project and several files in it.
Figure 2. The Solution Window of the FirstWindows application
While the file names are different, you can see lot of similarities in the approach to creating a Web Project with ASP.NET and C#, as discussed in the following article: First Web Program (Web Project) in C# Explained
Let’s discuss the important files generated for the Windows Application below.
FirstWindows.sln and FirstWindows.suo
We named our application FirstWindows. You will see a Solution file named FirstWindows.sln. This is a text file that contains the information about the projects included in this solution.
If you double-click on the .sln file in the Windows Explorer, it will be opened in Visual Studio, as the .sln files are associated with Visual Studio.
If you go to the root directory of the FirstWindows application, you will also see another file named FirstWindows.suo. This is a binary file – if you open it with Notepad, you will see gibberish (unlike FirstWindows.sln file – which is a readable text file).
This .suo file is specific to the current user using Visual Studio. If you are working in a team environment, this suo file will different for other users (with their own preferences). So, you should not check the .suo file into the Version Control System.
FirstWindows.csproj is the project file for this application. Solution file, as discussed above, has a .sln extension and can contain multiple projects. The file with .csproj extension is a C# Project File.
Code Listing 1. The C# Project File
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Configuration Condition=" $(Configuration) == ">Debug</Configuration>
<Platform Condition=" $(Platform) == ">AnyCPU</Platform>
<PropertyGroup Condition=" $(Configuration)|$(Platform) == Debug|AnyCPU ">
<PropertyGroup Condition=" $(Configuration)|$(Platform) == Release|AnyCPU ">
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
As you can see in Code Listing 1, the project file is an XML text file. It contains a whole lot of information about the project, including:
- platform to compile this project to
- how to generate debug and release binaries
- the references to other assemblies included in this project
- the source files that are part of this
- the commands to run before and after the build
Form1.cs, Form1.Designer.cs, Program.cs
These are the C# files that are generated by Visual Studio. Typically, you will add more logic to Form1.cs file (after changing its name to something appropriate for your project).
The code in these files is discussed in the following section in detail.
In addition to the above files, several other files are also generated as a part of the solution.
This file contains the information about the assembly (the dll or exe). This information includes assembly title, assembly description, assembly version, assembly copyright, and so on.
These two files (Resources.resx and Resources.Designer.cs) contain resource information – like strings, etc.
These two files (Settings.settings and Settings.Designer.cs) contain the application settings (whether the scope is for user or for application).
In this section, let’s analyze the code generated as a part of some of the important files in this application.
Code Listing 2. Program.cs
static class Program
/// The main entry point for the application.
static void Main()
As shown in Code Listing 1, the Program.cs file contains the Main() method. This is the entry point for C# applications. This method uses the Application class from the System.Windows.Forms namespace. The System.Windows.Forms namespace is the main namespace that contains WinForms functionality.
The Application class provides a whole bunch of static methods that help manage an application. The last line in this method calls a method called Run with Form1 as the parameter.
As required in every Windows program, this Run() method starts the main message loop for the application. It also shows the Form1 after that.
Form1.cs and Form1.Designer.cs
Code Listing 3. Form1.cs
public partial class Form1 : Form
Code Listing 4. Form1.Designer.cs
partial class Form1
/// Required designer variable.
private System.ComponentModel.IContainer components = null;
/// Clean up any resources being used.
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
if (disposing && (components != null))
#region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
private void InitializeComponent()
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
The code shown in Code Listings 3 and 4 is the basic code for showing a form/screen in a Windows Forms application.
One of the first things you would do is to change the name of the form from Form1 to something else that is meaningful for your application.
Form1.Designer.cs is where the Visual Studio Form designer will keep adding code as you add more controls to the form. And the Form1.cs is where you write the code.
While you ‘draw’ the user interface on Form1, those controls (like labels, textboxes, buttons, etc.) will be created in code in the InitializeComponent() method in Form1.Designer.cs file.
And, as you can see, this method is called from the constructor of the Form1 class.
Run the program
We will add just one label to the form and set it to: Hello from FirstWindows.
Figure 3. Label to display on Form1.
Simply drag the Label from the Toolbox and drop it on to the design surface of Form1. Then from the Properties window, set the Text property to Hello from FirstWindows.
If you run the above application (by pressing F5), you will see the window shown in Figure 4 with the label displayed.
Figure 4. Form with a label