Visual Basic 2008 is the latest iteration of Microsoft’s visual basic programming series. This article shall focus on VB2008 express edition, which is completely free and downloadable from here

The most noticeable difference between previous versions is the ability to create programs for web situations, which was not so prominent beforehand.
In order to create a new project and application, once the main screen is loaded, do the following:
Select File>New Project ( or CTRL +N)
You will then see a dialog box, identical to the one below.
[insert image]
For this tutorial, select Windows forms Application – this is what you would have normally done in previous iterations of the program. For completness, the other options are:
  • Class library - these are simply ways of writing modules, or blocks of code that do one specific thing and cannot execute themselves as programs, but can instead be called by other programs. This process is known as linking. Dynamic Link Librarys (DLLs) are an example, and are used by many major software titles, include Microsoft applications.
  • WPF Application – just another type of application that can run either on the desktop or directly online from a webserver as an online application. Utilises DirectX graphic capabilities to allow you to create clever GUI’s for you program.
  • Console Application - a computer program designed to be used via a text-only computer interface, such as a text terminal, the command line interface of some operating systems (Unix, DOS, etc.)

What is also slightly different with vb2008 is the fact that for the most part, your project saves alongside the program files, in so far as it can’t be opened from My Documents or the Desktop. However, once you publish the application (which shall be covered later) you can save to any location, including directly to a web server.
So with the basics of setting up a project covered, we shall move onto actually writing some code.
In this article we shall cover some of the basics of Visual Basic programming. Despite what the name of the programming suggests, Visual Basic can be used for very complex programs.
BASIC stands for Beginner's All-purpose Symbolic Instruction Code, and the visual element is due to the fact that the programming relies on a graphic layout, as the Tutorial shall demonstrate later.
The first bit of coding we shall look at is setting up a messagebox to display a message to the User.

Hello World Msgbox
Code:
Code:
Msgbox       [Prompt as object],  [buttons as Microsoft.visualbasic.msgboxstyle], [Title as object

  • Prompt is the actual text you want the msgbox to display.
  • This first major variable tells the compiler (visual Basic) which type of buttons or sometimes the style you want on the MsgBox. After entering the prompt and a Comma and a space, you get a dropdown box of all the possible options here. It’s easier for you to do this by trial and error, as most of these are fairly self explanatory, than to simply exhaustively list all of them.
  • The title variable, as the name suggests, allows you to change the title text displayed in the blue bar at the top of the message box.
    The words As object and As Microsoft.visualbasic.msgboxstyle simply tell the compiler what these specific things are and what sort of data these will be. When you enter the prompt text, you do not need to addas object to the end; in fact this shall return an error, causing your program to fail to build.

So, now you understand all the components of this statement, let's code a Msgbox command. Firstly, we must put a button on the window of our program, for the user to click in order to generate the message box. To do this, on the main window, look to the left of the screen. There is a toolbar down the side called Toolbox. Put your cursor over this. The toolbox should then expand to fill the left hand side of the screen. It should look like the following image.


A few down from the top is the item we are looking for, called Button. Click this and it turns orange, as shown below.


Simply move the cursor over the form of our project behind the toolbox and click and drag to insert the button. Once placed it can be moved to whatever position works best.
The end result should look like this (obviously the placement of the button is arbitrary)


So that we know what this button actually does, we need to change the text on it. To do this, go to the properties window, in the bottom right of the overall window. Ensure the button is selected and the properties for this button shall be displayed here. The one we are interested in is called Text. By Default the text is Button 1 but just simply type in the box to change this. Then once you have changed it to what you want, click anywhere else and it will update the button itself. For this example,Click here is used as the text.
Now we have the user interface sorted, we need to put the code itself in to make the button do anything. The user interface is simply any part of your program that the user can see or interact with.

Adding code

Double click on the Command button and you will be presented with your first page of code. By double clicking on that button, the compiler adds the start and end bits of code that allow that button to do anything. These can be added by hand, but any tiny mistake will stop the program working and finding these kind of mistakes is not as simple as it sounds.
Code:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub
For now, we do not need to know how to utilise the arguments passed, but as a program becomes more and more complex they can be utilised.

Then add the message box code
Code:
Code:
MsgBox("Hello World", MsgBoxStyle.OkOnly, "My First Program")
In between the private sub and end sub code as above. This should result in the following:
Code:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("Hello World", MsgBoxStyle.OkOnly, "My First Program")
    End Sub
Once you’ve added this code, you’ll need to run your project in order to test that it works. Click the green arrow, as shown below.


After a few seconds, whilst your program is compiled and tested for errors, the program shall load. Click the button and your message box message should appear.

Congratulations, you have just successfully compiled your first visual basic program.
The next section looks at conditional programming, a crucial part of any program with user intervention.

Conditional programming

Unless you are programming a completely static program, where only one set of outcomes can arise, you will need to use conditional statements of some kind. The most basic type is the IF statement, and this is how it works and how we shall use it.
Code:
Code:
The logic of an If statement is as follows:
If Statement is (whatever we choose to test, usually true or false) Then do the following
Code
Else ( the above statement resolves as false)
Do this code
End the if statement
This is pretty much exactly the code we use. To make this really effective, we will be combining If statements with the msgboxwe did above (with a bit added)
Project: The idea of this project is to display a Msgbox, to capture the result of this MsgBox and display a relevant response in the program.
The first job is to create variables, pieces of data stored by the program that allow us to do something dynamically and store responses to certain events in the program.
Firstly, we must navigate to the code screen, as such
View | Code (or F7 to get straight to this screen)
You should see the first line says Public Class Form 1 then a line and then the code we inputted above.
In this spare line, we need to create our variable. This space is known as General Declarations and it’s where all global variables are created. The use of global is simply because it is possible to have local variables as well, but for this project we do not need to do so. The code to create our variable is as follows:
Code:
Code:
Dim response As Integer
The Dim bit tells the compiler we want to define a variable here, response is the name of the variable and As integer tells the compiler that we shall be inputting a number into this variable at some point. Notice that the variable doesn’t contain any data yet, it simply exists. You will need this later.
Still in Code view, go back to this sub we wrote earlier
Code:
Code:
Private Sub Command1_Click()
Msgbox “Hello World”, vbOkOnly, “Hello World”
End Sub
The changes needed to get this to store a result and then turn it into something the user can see, are small, comparatively.
Code:
Code:
response = MsgBox("Hello World", vbYesNo, "Hello World")
All we are doing here is setting the result(an integer in this case) of the msgbox as our variable, so we can do something with this input. Once this is run, our integer will hold a value.
To display some useful outcome to the user we then need to interpret the value of response. Yes or No are represented in a msgbox by either a 6 or 7 respectively. After the msgbox code, add the following.
Code:
Code:
If (response = "6") Then
MsgBox ("Yes")
Else
MsgBox ("No")
End If
If we were to take this to its extreme, we would really need an ElseIfstatement instead of the Else statement to check the other value was indeed a 7. However, in this case, the value cannot be anything else, so this is good enough for our purposes.

© 2010 techsupportforum.com