Learning to Code in Visual Basic
I will be using various terms during this tutorial here is a small reference centre for you:
VB/VB200 8 = Visual Basic 2008
GUI = Graphical User Interface
Form = Application with a GUI
1) Firstly we need to download the required software:
Microsoft Visual Basic 2008 Express Edition - Software Informer. Visual Basic 2008 Express Edition SDK for developping .NET applications.
The software is free of charge as it is by Microsoft, you will realize when you download you will receive a notification “This is a Trial Version, You have 30 days left” Don’t panic all you need to do is register with your email address and you will no longer receive this notification.
2) When you have downloaded Visual Basic 2008 you will be greeted with this screen:
What we are greeted with is our “Start Page” – This is where we can view the news about VB2008 and view our recent projects.
Part Two
What are we going to be coding?
3) We are going to be coding a computer information center, this will tell us various information about our PC.
Our plan is when a user clicks a button it will full up empty text boxes that we have placed with the required information.
Many people make the mistake of thinking Visual Basic is an easy coding language because it is drag and drop, I can tell you they wrong it may be drag and drop however do they see the coding that goes in behind the GUI?
Part Three
Right, let’s get started!
4) Firstly we need to start a new project:
We are greeted with this:
5) When we are coding with VB and we want to make an application with a GUI we want to create a “Windows Forms Application” I will go into more detail later about what the others do.
6) If we click “Windows Forms Application” once, at the bottom of the page we can give our project a name as we are going to be coding an Information Center I’m going to call mine “Info Center” nice and original 
Now we have made our project.
Part Four
What’s on my screen?
Don’t panic im going to break this down :)
7) On the right hand side of the program we have our “solution Center” and our “properties” I always close the solution center as I have never used it unless I am working on a huge project. Once we have close that “Data resources will pop up as well, close that now our “properties” has more room.
Part Five
Let’s get Geeky!
Click once in your form (“FORM1”) this has now made if active meaning anything we change in the properties tab will affect our Form (our program).
8) Let’s change the Forms name to our program name.
Scroll to the bottom of the “properties Tab” until you see “Text” change this to “Info Center”
Now we have changed that the title of our program has changed.
9) If we now click button 5 the Run program button it will start our program for us, however this is very boring as our program doesn’t do anything yet!
Note
when we run our program in order to edit and make changes we need to stop running the program we can do this 2 ways!
- Click the red Cross button in our program
- Click button 6 (Stop program)
10) On the left hand side we have our tool box, this is where we can add things to our program.
11) Lets add 6 text boxes to our program!
Click on the Toolbox then click on “Text box” then click on your form 6 times!
If we now run our program again we have 6 working text boxes !
12) Lets make them look professional, drag and drop them until they look something like this.
Tip
Remember we need to stop running our form before we can edit it.
13) Now let’s add 6 Text Labels, and make them look professional. We will change what they say later after we have coded our program:
14) Now we need to add 2 buttons to our Form (Application), Using the Toolbox add 2 buttons:
15) Double click button 2 and it will open the coding screen for button 2.
We can see this code:
Script:
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End Sub
End Class
Im going to break this down, as the code is the same in anything to do with VB.
Script:
Public Class Form1
This section of code is our program meaning Public class Form1 is the start and End class is the End. Form1 is our programs name (name within the code not the name the users see).
Script:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
The section of code is telling our program that is a Private sub for button 2. The button2_click means that when button 2 is pushed it will perform an action, an action that we code below.
Script:
Private Sub Button2_Click
Private subs work the same a Class’s when we have a class we must end it without “End Class” the same applies for a sub we must end it with “End sub” Simple aye?
Don’t worry its confusing but by the end of this tutorial it will make perfect sense.
16) Now button 2 we would like to close our program, it’s going to be an Exit button.
So if we look at this code:
Script:
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End Sub
End Class
What is saying is the program starts here, button 2 gets pushed it ends here. We have coded nothing.
If we add “Close()” in buttons 2 section when the button is pushed the form will close, it should look something link this.
Script:
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Close()
End Sub
End Class
If we now run our program when we push button 2 the form will close.
17) Now we need to tell the text boxes to do something when button 1 is pushed:
There are many ways to do this however the simplest way is the way I’m going to show.
If we double click Button 1 we can see button 1’s coding:
This is what you should see:
Script:
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class
For this we are going to use this command: But we need to tell the button were it is going to output the information that is why we added 6 text boxes.
Under button 1’s section add this:
What this means is when button 1 is clicked textbox1’s text is going to equal something.
When the user pushes the button I would like textbox 1 to show them there computer name, we are going to do this using the “my.computer.name” command.
Script:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
If we now run our program and click button1 it will show us our computer name!
18) We now to repeat this process for all of our 6 text boxed but giving those different commands as we go along.
Here is what I have:
Script:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = My.Computer.Name
TextBox2.Text = My.Computer.Info.OSFullName
TextBox3.Text = My.Computer.Network.IsAvailable
TextBox4.Text = My.Computer.Info.OSPlatform
TextBox5.Text = My.Computer.Clock.GmtTime
TextBox6.Text = My.Computer.Info.TotalPhysicalMemory & " Mb"
End Sub
I am using the same command just with different endings, the great thing about using the software is that it will give you hints.
If we look at the last line of code I have done:
Script:
TextBox6.Text = My.Computer.Info.TotalPhysicalMemory & " Mb"
At the end of the code I have By doing this I am asking the output to put “MB” on the end of the result, by placing “” in between the MB I am telling the program that that is a comment I would like to put in the text box.
Notice how I have put all the command in the Button 1 section meaning when the button is pushed I would like all the commands to run.
Now let’s run our program and push button 1.
19) Now let’s clean up our program.
Firstly we need to give our labels some text that reflects what information will be in the text boxes. To do this we need to click on Label 1 then look in the properties for label 1.
Once you have done that scroll down in the properties field until you see “text” change this to “Computer name”
Do this for each label all 6 of them.
And you will have something that looks like this:
20) Now we need to the same for the buttons,
21) If we select everything on the form we can move it, I’m going to move it more in the center. Then I’m going to drag the Form up a little as it’s too long.
You should have something that looks like this now:
You have just made your first program!