J#.Net Tutorials

J#.Net Tutorial #9

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

package Project1;

import System.*;
import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;

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

	public WinForm()
	{
		InitializeComponent();
	}

	protected void Dispose(boolean disposing)
	{
		if (disposing)
		{
			if (components != null)
			{
				components.Dispose();
			}
		}
		super.Dispose(disposing);
	}

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

		this.button1.set_Location(new System.Drawing.Point(8, 32));
		this.button1.set_Name("button1");
		this.button1.set_Size(new System.Drawing.Size(88, 24));
		this.button1.set_TabIndex(0);
		this.button1.set_Text("button1");
		this.button1.add_Click( new System.EventHandler(this.button1_Click) );

		this.textBox1.set_Location(new System.Drawing.Point(8, 8));
		this.textBox1.set_Name("textBox1");
		this.textBox1.set_Size(new System.Drawing.Size(88, 20));
		this.textBox1.set_TabIndex(1);
		this.textBox1.set_Text("textBox1");

		this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
		this.set_ClientSize(new System.Drawing.Size(116, 68));
		this.get_Controls().Add(this.textBox1);
		this.get_Controls().Add(this.button1);
		this.set_Name("WinForm");
		this.set_Text("WinForm");
		this.ResumeLayout(false);

	}

	public static void main(String[] args) 
	{
		Application.Run(new WinForm());
	}

	private void button1_Click (Object sender, System.EventArgs e)
	{
		this.textBox1.set_Text("Hello");
	}
}

Wow, quite a lot. Still, compared with a Windows program written in C, it's pretty small. Compile it in the usual way but using the references you used in the messagebox example. 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...

Update Regrettably, I have just discovered (having slaved over these tutorials) that Microsoft will be dropping J# from Visual Studio so 2005 is the last version (nobody seems to be using it) although it is being supported with redistributables till 2015. Frankly, it is so close to C#.net that you might just as well use C# and it should be fairly easy to convert. I've also discovered that Visual Studio 2003 (or any of its individual parts) won't run on Windows Vista (yes, thats Windows Vista! - you'll have to buy an upgrade!).

Back to J# Tutor

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