C Sharp Programming/The .NET Framework/Windows Forms

System.Windows.Forms.
To create a Windows desktop application we use the library represented by namespace. Some commonly used classes in this namespace include:
Form class.
The class (System.Windows.Forms.Form) is a particularly important part of that namespace because the form is the key graphical building block of Windows applications. It provides the visual frame that holds buttons, menus, icons, and title bars together. Integrated development environments (IDEs) like Visual C# and SharpDevelop can help create graphical applications, but it is important to know how to do so manually:
using System.Windows.Forms;
public class ExampleForm : Form // inherits from System.Windows.Forms.Form
 public static void Main()
 ExampleForm wikibooksForm = new ExampleForm();
 wikibooksForm.Text = "I Love Wikibooks"; // specify title of the form
 wikibooksForm.Width = 400; // width of the window in pixels
 wikibooksForm.Height = 300; // height in pixels
 Application.Run(wikibooksForm); // display the form
The example above creates a simple Window with the text "I Love Wikibooks" in the title bar. Custom form classes like the example above inherit from the class. Setting any of the properties , , and is optional. Your program will compile and run successfully, if you comment these lines out, but they allow us to add extra control to our form.
Events.
An event is an action being taken by the program when a user or the computer makes an action (for example, a button is clicked, a mouse rolls over an image, etc.). An event handler is an object that determines what action should be taken when an event is triggered.
using System.Windows.Forms;
using System.Drawing;
public class ExampleForm : Form // inherits from System.Windows.Forms.Form
 public ExampleForm()
 this.Text = "I Love Wikibooks"; // specify title of the form
 this.Width = 300; // width of the window in pixels
 this.Height = 300; // height in pixels
 Button HelloButton = new Button();
 HelloButton.Location = new Point(20, 20); // the location of button in pixels
 HelloButton.Size = new Size(100, 30); // the size of button in pixels
 HelloButton.Text = "Click me!"; // the text of button
 // When click in the button, this event fire
 HelloButton.Click += new System.EventHandler(WhenHelloButtonClick);
 this.Controls.Add(HelloButton);
 void WhenHelloButtonClick(object sender, System.EventArgs e)
 MessageBox.Show("You clicked! Press OK to exit of this message");
 public static void Main()
 Application.Run(new ExampleForm()); // display the form
Controls.
The Windows Forms namespace has a lot of very interesting classes. One of the simplest and important is the 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.
using System.Windows.Forms;
using System.Drawing;
public class ExampleForm : Form // inherits from System.Windows.Forms.Form
 public ExampleForm()
 this.Text = "I Love Wikibooks"; // specify title of the form
 this.BackColor = Color.White;
 this.Width = 300; // width of the window in pixels
 this.Height = 300; // height in pixels
 // A Label
 Label TextLabel = new Label();
 TextLabel.Text = "One Label here!";
 TextLabel.Location = new Point(20, 20);
 TextLabel.Size = new Size(150, 30);
 TextLabel.Font = new Font("Arial", 12); // See! we can modify the font of text
 this.Controls.Add(TextLabel); // adding the control to the form
 // A input text field
 TextBox Box = new TextBox(); // inherits from Control
 Box.Location = new Point(20, 60); // then, it have Size and Location properties
 Box.Size = new Size(100, 30);
 this.Controls.Add(Box); // all class that inherit from Control can be added in a form
 public static void Main()
 Application.EnableVisualStyles();
 Application.Run(new ExampleForm()); // display the form