Visual Basic Introduction

Visual Basic is a graphical programming environment that allows for rapid prototyping of designs.  Visual Basic is an event driven environment, meaning all controls, objects, or widgets that you place in a window or form come with their own events that can be fired either automatically or by some user action, such as a mouse click or key press.

Download a sample VB program that checks data entry from a user.

Instructions for Creating the Example:

  1. Start Visual Basic from the Start menu.
  2. Select Standard EXE, and click Open.
  3. VB should now show a window with: the Toolbox, a Form, the Project Explorer, and the Properties Window.

The Toolbox lets you select widgets or objects to place on a formThe Project Explorer is a listing of all forms or code modules that belong to your project.  The Properties toolbar allows you to customize various properties for each widget.

If any of the toolbars are not visible, go to the View menu to show them.

  1. We are first going to add some more widgets/controls to our toolbox.  Go to the Project menu then Components.  Select Microsoft Windows Common Controls 6.0 and click OK.

  1. Place the following widgets on your form: two command buttons , three labels , one slider , and two textboxes .  Select them from the toolbar and then draw a rectangle on the form.  You can move them around and resize them as you like.

  1. Set the properties of the form by clicking on it to select it and then going to the properties toolbar.  Change the name property to frmMain and the caption property to "My First VB Program".

  1. Set the following properties of each widget by clicking on the object in the form and going to the properties toolbar.
Command Button 1 Command Button 2 Label 1 Label 2 Label 3
Name cmdCheck cmdExit lblName lblAddress lblAge
Caption Check User Exit Name Address Age

 

Text Box 1 Text Box 2
Name txtName txtAddress
Text <make it empty> <make it empty>

 

Slider 1
Name sliAge
Min 0
Max 30
Value 15
 

It should look something like this:

  1. We are ready to add some code now.  Code will run when an event happens to an object, such as clicking it or typing into it.  First let's add code to the exit button, just double click on it.
  2. A subroutine is created where we will add our code.  Notice that the subroutine is for the object cmdExit and code placed here will run when the object is Clicked.  You can change the event with the drop down box in the top right corner.  This will cause your code to run when a different event occurs.

  1. Add the following code to the cmdExit_Click() subroutine:

Private Sub cmdExit_Click()

    End
End Sub

  1. Double click on the form to add code to a special subroutine called Form Load.  Here is a good place to initialize variables or control properties.  Add the following code to Form Load:

Private Sub Form_Load()
    ' this subroutine runs when your form first appears

    ' we will change some properties of our controls at startup

    ' to access a control's property, type the name of the control, then a period, and
    ' finally the property's name

    txtName.Text = ""
    txtAddress.Text = ""
    sliAge.Value = 15
End Sub

  1. You can add code for the rest of the objects the same way; just double click on each object.  The code is as follows:

Private Sub cmdCheck_Click()

    ' check the text property of txtname
    If txtName.Text = "Carman" Then
        MsgBox "You rock!", vbOKOnly, "Name Validation"
    Else
        MsgBox "Error", vbCritical, "Name Validation"
    End If

    ' check the value property of sliAge
    If sliAge.Value < 5 Then
        MsgBox "Youngin'", vbOKOnly, "Age Validation"
    ElseIf sliAge.Value >= 5 And sliAge.Value < 18 Then
        MsgBox "How's school going?", vbOKOnly, "Age Validation"
    Else
        MsgBox "Start planning your life...", vbOKOnly, "Age Validation"
    End If

    ' reset our controls' properties
    txtName.Text = ""
    txtAddress.Text = ""
    sliAge.Value = 0

End Sub

  1. To run your program, click on the Play button in the toolbar under the menu.