![]() |
JScript.Net Tutorial #9
And now we get to program Windows GUI applications. Here's some code:-
import System;
import System.Windows.Forms;
import System.ComponentModel;
import System.Drawing;
package WinForm
{
class TestForm extends System.Windows.Forms.Form
{
private var textBox1: TextBox;
private var button1: Button;
function TestForm()
{
InitializeComponent()
}
private function InitializeComponent()
{
textBox1 = new TextBox();
button1 = new Button();
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";
button1.add_Click(button1_Clicked);
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";
ResumeLayout();
}
private function button1_Clicked(o : Object, e : EventArgs)
{
this.textBox1.Text = "Hello"
}
}
}
Application.Run(new WinForm.TestForm());
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
Unfortuntely, you can't program in JScript with any of the above. You can use Visual Studio BUT you can't run code or compile it from within the IDE and you CAN'T create winforms visually (yes, I know...I've only just found out after having done all these tutorials!). However, if you really must use JScript then you can create the winforms in a language like C#.net (or VB.Net) and copy the relevent code into a JScript file although I admit it isn't perfect.
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...