C# Tutorial - Hello World Windows StyleCreate a new Windows Forms Application called WApp01 using the New Project dialog. When the application is created, the IDE shows a blank form called Form1 in the Forms Designer window. The title bar of the form has been assigned the name of the form, which by default is Form1 and is otherwise a blank canvas for us to work on. Open the Code Editor window by right-clicking on the form and selecting View Code, alternatively you can use the keyboard shortcut F7 or select Code from the View menu. When it opens you will see the following code: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WApp01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
This is only half of the code required for the form class as can be noted by the use of the partial keyword, but we will discuss the other half of the code shortly. The first thing to note is that when the IDE creates a new form, it includes many of the System namespaces, making the assumption that you are going to create a large application that will use them. Don't worry about this as they will not make the executable much bigger than if they were not included. The only method that is exposed to us at this point is the constructor of the form. Within the constructor it calls one method that is located in the other half of the code that we can not easily see. To open the other half of the code, open the solution explorer and click on the plus sign next to the class Form1. This will now display the file Form1.Designer.cs that we can double-click on to open. This file contains two methods and a container that will hold all of the components that will be added to the form. Components will be discussed later on. namespace WApp01
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed;
/// otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
}
#endregion
}
}
The Dispose method is where you dispose of managed and unmanaged resources used by the form. If the parameter disposing is true, then the managed resources used by the form will be disposed of. If the parameter disposing is false, only the unmanaged resources used by the form will be disposed of. With unmanaged resources, you must call their dispose method manually. For this tutorial, we will not be looking at this subject matter greatly. The InitializeComponent method is where the controls and components are actually instantiated (with the new keyword) and their properties are initialized. It is also the area where any properties of the form itself are initialized. Since the control variables are members of this class, the IDE appends the this keyword; however, it is not required. A new Windows Forms application also has a Program.cs file just like a console application. This file contains the Main method that runs the form. using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WApp01
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
STAThread stands for Single Threaded Appartment Thread. This means in the process (application), there is only one thread which will accomplish all of the tasks for the process. Before we run the application, we need to compile it. This can be achieved by choosing Build Solution from the Build menu, or by pressing the shortcut key F6. If the application has any errors, they will be displayed in the Output window. Because we are building a simple application, then we do not expect any errors and we can run the application. To run the application choose Start Debugging from the Debug menu, or use the shortcut key F5, or click on the green arrow in the toolbar. The application will run with an empty form. ![]()
|