Home / Software / Visual Basic hello World Tutorial

Visual Basic hello World Tutorial

Today I will teach you about how to use Visual Studios to write your own windows program using the language Visual Basic. The code that we will use is compatible with of Visual basic 2003, 2005, 2008, and 2010. The syntax of visual basic is very easy to learn, it’s great for beginners like I was not too long ago. So let’s begin with the programming.

First, let’s open up Visual Studio, then select “New Project…” after you have done that choose “Windows Form Application” from the list, and name it “Hello World”. Once Visual Studio loads, a form designer window will show up, go ahead and close that out as I’ll be teaching you how to create controls and set properties at runtime.

So now have a look in the Solution Explorer to your upper-right, then right click on Form1 and select “View code”.

Now let’s begin to write the program. The code automatically generated by Visual Studios should look something like this.

Public Class Form1

End Class

The Form1 Class is like a shell that holds all the instructions inside, which are known as Subs. Your Subs are like little bits of instructions that do a certain task. For example if we were to write a program to brush your teeth, the class would be Public Class Brush Teeth. You might have a Sub named Apply_Toothpaste() or Scrub_Teeth(). These events can have handlers such as Scrub_Teeth() handles Me.Applied Toothpaste.

What we’re going to do is next create a new Sub. This Sub will be a automatically generated Sub named New. So here’s what our Sub should look like. This Sub get processed when the application is first processed by windows, before its even shown on the screen.

Simply place your cursor between Public Class Form1 and End Class, then type “Sub New” and press the enter key.

Public Class Form1

Sub New()

InitializeComponet()

End Sub

End Class

Now from this Sub we will set the forms properties. The “InitializeComponet()” is automatically added when you press the enter key after typing Sub New(). Now le’ts review the code we have so far. The code Me.Text simply means Me being Form1, and its window text being “Hello World”. Me.ClientSize is the actual size of the form inside of the windows borders. Me.Maximizebox is the resize and maximize box at the top right of the form, by setting it to false it disables it. Msgbox(“Hello World, This is my first program!”) displays a MessageBox with the text “Hello World, This is my first program!”. End Sub simply shows that your finished with your instructions, And End Class shows your done with Form1’s instructions.

Public Class Form1

Sub New()

InitializeComponet()

Me.Text = “Hello World”

Me.ClientSize = New Size(400,300)

Me.Maximizebox = False

Msgbox(“Hello World, This is my first program!”)

End Sub

End Class

Congratulations! Now press F5 on your keyboard, your computer will compile your program and run it. If all was done right, your program should compile perfectly and run. You have now learned how to set form properties at runtime and you have successfully written your first hello world program in Visual Basic.

About User Lin