Visual Basic for Beginners

If you want to learn how to program in Visual Basic, this tutorial can help you get started in a few simple steps. Visual Basic (or VB) is a great programming language commonly used for developing Windows programs and applications. It is also used as a server side programming language on the ASP.NET platform along with C# and other languages supported by the .NET framework.

How to Install Visual Basic

Before you can start programming and writing Visual Basic code, you need to install Visual Basic on your Windows computer. There are two common ways you can install Visual Basic:

  • CD installer
  • Installer downloaded from online

You can get a free CD installer if you buy some Visual Basic books as well, so always make sure to check whether or not your books include an installer CD.

To Install Visual Basic Express using Installer CD:

  • At the back of your book, carefully extract the CD and put it in your computer CD drive.
  • Do not use the auto-play/auto-run feature; if it pops up after inserting the CD, simply close it.
  • Go to My Computer and then double click on your CD drive. Next, double click the VBExpress folder.
  • Close all open/running applications on your computer (e.g. MS Office applications, browsers, Windows Media Player, etc).
  • There are a lot of files inside the VBExpress folder - double click setup.exe. This will start the installation process. Follow the prompts and stick with the default installation options.
  • Once everything has been installed, you will receive confirmation that Microsoft Visual Basic Express 2008 has been successfully installed. Go to Start and click “Microsoft Visual Basic 2008 Express Edition”.

This is what it looks like:


To Install Visual Basic Express using online downloaded installer:

Note: The “express edition” has limited features compared to the full edition. However, for the sake of introducing you to Visual Basic and creating simple VB projects, an express edition will suffice.

Programs vs. Project Files and Solution

One important thing that you should know before we delve into coding is the development cycle of a Visual Basic application. The following are common development sequences:

Step 1.) Creation of Project files and solutions – these are the files that you will create in Visual Basic Express IDE (whether you are using the 2008 or 2010 version). These files contain the entire source code of the application and will only run in computers with Visual Basic Express installed.

Some of these files can take the form of a DLL (dynamic link libraries). As a developer, you will deal with the project files and solutions most of the time from within project development.

Step 2.) Generation of a program (.exe file) – this is the distributable component of the application. This is also known as the “installer” of the application. Users can deploy your application on their own computer and install it using the .exe file generated.

Your user does need to have Visual Basic Express installed on their computer. However, examples illustrated in this tutorial will focus on running the programs from within the Visual Basic Express IDE.

A “Hello World” Example

To quickly introduce a very simple programming example in Visual Basic, let’s suppose you want to create a console application that, when run, displays the message “Hello World” to your screen.

A console application is a project template type in Visual Basic that is created solely for a text based interface such as command line methods or in DOS. There are a lot of project types in Visual Basic that you can use and which we will discuss later on.

Step 1.) Go to Start  All Programs — > launch Microsoft Visual Basic 2008 Express edition.

Step 2.) Go to File  New Project  and you will see 5 different project templates/types that you can use. These are as follows:

  • Windows form application – used when you want to create programs that will accept user input. This is a GUI implementation for a user friendly application.
  • Class Library- used when you need to create a VB class.
  • WPF Application – when you need to create Windows Presentation Foundation applications: http://msdn.microsoft.com/en-us/library/aa970678.aspx
  • WPF Browser Application – used when creating Windows Presentation foundation web browser applications: http://msdn.microsoft.com/en-us/library/aa480223.aspx
  • Console application – a project for creating a command line implementation.

 

Select “Console Application” and then in the project name type HelloWorld. Finally, click OK. VB express will generate a file called “Module1.vb”, with the following code:

Module Module1
    Sub Main()

    End Sub
End Module

Your code needs to be placed between “Sub Main ()” and “End Sub”. These are called “subroutines”, which are similar to a function except that a function can return a value.

Since you are going to display the “Hello World!” message to your screen, you need to use the MsgBox function: http://msdn.microsoft.com/en-us/library/sfw6660x(v=vs.85).aspx to display this message.

This is the final code:

Module Module1

    Sub Main()
        MsgBox("Hello, World!")
    End Sub

End Module

7.) You can this code by pressing the F5 key. This is how the output looks:

To stop the program and to exit, just click “OK”.

To save the example program, go to File  Save all. VBExpress will ask you for the saving path. Make sure “Create directory for solution” is checked. Finally click “Save”.

Try closing the project (File – Close Project), then go to the start page and you should see the “HelloWorld” project under “Recent Projects”.

{mospagebreak title=Creating a Visual Basic Form}

Creating a Visual Basic Windows Form Application – Hello World Example

Let’s illustrate how to create a very basic Windows form application in Visual Basic:

Step 1.) In the VB Express IDE, go to File – New Project and then select “Windows Form Application”.
Step 2.) Name the project "HelloWorldFormApplication" and then click OK.
Step 3.) What you should see is an empty form named “Form1”. Suppose you want to add a button to it so that when it’s clicked it will display the text “Hello World”.

The first thing you need to do is add the button. Go to View  Toolbox. Under “Common Controls” click and drag “Button” to the center of Form1. Click “Button1” and under “Properties” (you can see the properties below the Solution Explorer) change the Text value from “Button1” to “Click Me”.

Step 4.) Click any area outside the “Click Me” button and in the properties change the value of Text from “Form1” to “Simple_Form _Application”.

This is how it looks after applying those changes:

So far the form doesn’t do anything, so let’s give it some instructions so that when it’s clicked, it will output “Hello World” to the screen. To do this, let’s write some Visual Basic code. Start by double clicking “Click Me”. You should then see the code below:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub
End Class

Your code needs to be inserted between the Private Sub and End Sub. To display “Hello World”, this is the final code:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show("Hello, World!")
    End Sub
End Class

Note: MessageBox.Show is used to display “Hello World’ when the Click Me button is clicked.

Let’s try saving the application, go to File Save all. Now run the application by pressing F5. When the application form pops up try clicking “Click Me” and then you should see the “Hello World!” message box. Screenshot:

One thought on “Visual Basic for Beginners

[gp-comments width="770" linklove="off" ]