C# CSharp C#.Net Tutorial

C#.Net Tutorial #9

And now we get to program Windows GUI applications. Here's some code:-

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Project1
{
	public class WinForm : System.Windows.Forms.Form
	{
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox1;

		public WinForm()
		{
			InitializeComponent();
		}

		protected override void Dispose (bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();

			this.button1.Location = new System.Drawing.Point(8, 32);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(136, 24);
			this.button1.TabIndex = 0;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);

			this.textBox1.Location = new System.Drawing.Point(8, 8);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(136, 20);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "textBox1";

			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(152, 60);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.Name = "WinForm";
			this.Text = "WinForm";
			this.ResumeLayout(false);
		}

		static void Main() 
		{
			Application.Run(new WinForm());
		}
		
		private void button1_Click(object sender, System.EventArgs e)
		{
			this.textBox1.Text="Hello";
		}
	}
}

Wow, quite a lot. Still, compared with a Windows program written in C, its pretty small. Compile it in the usual way. So what's all this code and how does it work? Well...I'm not going to tell you. What! You see, you'd have to be mad to program a GUI program in Notepad! There really is only one way to do this and that's with a visual IDE (Integrated Development Environment). Now I know what you're going to say but really it doesn't have to cost you a penny. There are various free IDE's available:-

SharpDevelop is a very good free IDE
Visual Studio Express 2005 is a free IDE from Microsoft! Bit limited but does databases.
Borland C# Builder Personal Edition free for non-commercial use
You can even develop and run .Net apps on Linux using
Mono

That's it for these tutorials. Thanks for looking and I hope they were useful. There may be some more in the pipeline as I'm interested in .Net controls on web pages and remoting. In the meantime, you can find my other tutorials for other languages on my main site here. Remember, if you get stuck or need help then email me at the address below and I'l try to help. See you around...

Back to C# Tutor

Written by Nick Cheesman. Last updated: 01/06/2007
Please eMail me at:
nickjc@nickjc.co.uk