
Navigator : Home > Tutorials > Windows Programming Tutorials > ...
Introduction to Windows Forms 2 in C#
In this tutorial we will look at how to create a Windows Form and how we can dynamically change it's appearance through the code-behind, as well as look at creating a custom Exit Application button, and message boxes. C# version.
This tutorial will serve as an introduction to programming for the Windows platform. In this tutorial we will learn such basic things as how to create a Windows Form and how to dynamically change the Header Title and dimensions of the form and the background color, as well as how to make use of message boxes and how to create an exit application button.
When working in the code-behind of the form (Form1), we can reference the form itself simply using the keyword this. For example, we will resize the form when it is loading with the following code:
private void Form1_Load(object sender, EventArgs e) {
this.Text = "Change form title here";
this.StartPosition = FormStartPosition.CenterScreen; this.AutoScaleBaseSize = new Size(5,13); this.ClientSize = new Size(400, 200); this.MaximizeBox = false; } |
The first line of this code simply sets the Title of the Form. The other four lines of code set the position and dimensions of the Form.
Next, we will add functionality to detect when the user clicks anywhere on the Form, and tell them. First, we add the following in the Form Load event:
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
| this.Click += new EventHandler(ClickForm); |
Then we add the ClickForm Event Handler:
public void ClickForm(object ob, EventArgs e) {
MessageBox.Show("You clicked on Form Area", "Message box Title"); this.BackColor = Color.Red; } |
Finally, we will create a friendly Exit button for the user. And before the application exits, we will notify the user.
We drag a button onto the Form Design view from the toolbox, then double click it to create a handler:
private void button1_Click(object sender, EventArgs e) {
MessageBox.Show("This program will now close.","EXIT"); Application.Exit(); } |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
The entire code-behind will look something like this:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace WinFormsIntro_cs {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) {
this.Text = "Change form title here";
this.StartPosition = FormStartPosition.CenterScreen; this.AutoScaleBaseSize = new Size(5,13); this.ClientSize = new Size(400, 200); this.MaximizeBox = false;
this.Click += new EventHandler(ClickForm); } public void ClickForm(object ob, EventArgs e) {
MessageBox.Show("You clicked on Form Area", "Message box Title"); this.BackColor = Color.Red; } private void button1_Click(object sender, EventArgs e) {
MessageBox.Show("This program will now close.","EXIT"); Application.Exit(); } } } |
Looking for the VB.NET 2005 Version? Click Here!
Looking for more .NET Tutorials? Click Here!