VB.Net Tutorial

VB.Net Tutorial #9

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

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Namespace Project1

	Public Class WinForm
	Inherits System.Windows.Forms.Form
		Private textBox1 As System.Windows.Forms.TextBox
		Private button1 As System.Windows.Forms.Button
		Private components As System.ComponentModel.Container = Nothing

		Public Sub New()
			InitializeComponent
		End Sub

		Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
			If disposing Then
				If Not (components Is Nothing) Then
					components.Dispose
				End If
			End If
			MyBase.Dispose(disposing)
		End Sub

		Private Sub InitializeComponent()
			Me.button1 = New System.Windows.Forms.Button
			Me.textBox1 = New System.Windows.Forms.TextBox
			Me.SuspendLayout
			Me.button1.Location = New System.Drawing.Point(8, 32)
			Me.button1.Name = "button1"
			Me.button1.Size = New System.Drawing.Size(136, 24)
			Me.button1.TabIndex = 0
			Me.button1.Text = "button1"
			AddHandler Me.button1.Click, AddressOf Me.button1_Click
			Me.textBox1.Location = New System.Drawing.Point(8, 8)
			Me.textBox1.Name = "textBox1"
			Me.textBox1.Size = New System.Drawing.Size(136, 20)
			Me.textBox1.TabIndex = 1
			Me.textBox1.Text = "textBox1"
			Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
			Me.ClientSize = New System.Drawing.Size(152, 60)
			Me.Controls.Add(Me.textBox1)
			Me.Controls.Add(Me.button1)
			Me.Name = "WinForm"
			Me.Text = "WinForm"
			Me.ResumeLayout(False)
		End Sub

		Shared Sub Main()
			Application.Run(New WinForm)
		End Sub

		Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Me.textBox1.Text = "Hello"
		End Sub
	End Class 
End Namespace

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 VB.Net Tutor

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