The Windows Forms namespace has a lot of very interesting classes. One of the simplest and important is the Form class. A form is the key building block of any Windows application. It provides the visual frame that holds buttons, menus, icons and title bars together. Forms can be modal and modalless, owners and owned, parents and children. While forms could be created with a notepad, using a form editor like VS.NET, C# Builder or Sharp Develop makes development much faster. In this lesson, we will not be using an IDE. Instead, save the code below into a text file and compile with command line compiler.
In the first example below, we display a gray form untitled "How do you do?".
//A very simple form
using System.Windows.Forms;
class test : Form //class test inherit from the class Form
{
public static void Main()
{
test HelloForm=new test();
HelloForm.Text="How do you do?";//specify title of the form
Application.Run(HelloForm);//display form
}
}
Notice that, like any other executable, the Windows Forms application has a Main entry point. There are two differences between a console application and a Windows Forms application. First, the class containing the main form inherits from System.Windows.Forms. This inheritance provides plumbing to create and run a Window. Lines and lines of code of this plumbing are hiding behind a single line Application.Run(HelloForm). You see, the Windows operating systems is using message loops to communicate with all open windows. As long a form is visible, its message loop receives and processes messages from Windows. Messages that a Form receives could be of different character. Some indicate that Windows are about to shut down, others simply notify the form that it needs to repaint itself, that it has been clicked and that the mouse is moving over it. As I said, all of this plumbing is passed in through Run method by a class that must inherit from System.Windows.Forms.Form
Below I create a Form containing a TextBox. Note, that the TextBox and the Form are created separately and then attached to each other with Controls.Add method.
//A very simple form with
attached textbox
using System.Windows.Forms;
class test:Form //class test inherit from the class Form
{
public static void Main()
{
test HelloForm=new test(); //create form
System.Windows.Forms.TextBox text=new
System.Windows.Forms.TextBox(); //create
textbox
text.Text="This is a textbox";// Specify default text
to be displayed inside TextBox
HelloForm.Controls.Add(text);//Attach TextBox to the Form
Application.Run(HelloForm);//display form
}
}
Finally, here is a program which creates a simple Check Box displayed on a yellow form:
//A very simple checkbox
using System.Windows.Forms;
class test:Form //class test inherit from the class Form
{
public static void Main()
{
test HelloForm=new test();
HelloForm.BackColor=System.Drawing.Color.Yellow; //set
the color of the form
System.Windows.Forms.CheckBox check=new
System.Windows.Forms.CheckBox();
HelloForm.Controls.Add(check);//Attach CheckBox to the Form
Application.Run(HelloForm);//display form
}
}
We will explorer other interesting classes belonging to Windows Forms namespace in
the next lesson. In the meantime, try creating barebones applications using
Radion Button and List Box. Good Luck!