Now we are ready to write our first program.  

Start a new Visual Basic standard exe project.

You should see a new blank form

Now add a command button

Now we need to tell the computer that when the cmdBeep button is clicked, the computer should make a beep sound.  Clicking the cmdBeep button is what is known as an event.  

In a traditional programming environment, things happen in a sequence determined by the programmer.  Visual Basic is an 'event driven' programming environment.  That means that the program responds to particular events controlled by the user, for example clicking on a button or pressing a key.  These events occur in an unpredictable order, depending on the user.  The screen is constantly being monitored by the computer waiting for the user to do something.

Each form or control object (eg command button) responds to a predetermined set of events.  For example these are some events recognised by a command button:

Event Action
Click Mouse clicks on button
KeyPress Key pressed
MouseDown Mouse button is pressed down
MouseUp Mouse button is released

Whenever an event occurs, Visual Basic runs the code (event procedure) you have written for it.  Procedures tell each object on your form how to react to something that the user does.  

You only need to write code for the events you want your program to respond to.  Event procedures can:

  • change an object's properties
  • make calculations
  • give messages
  • display dialogue boxes
  • send information to disk or a printer
  • start other procedures

Use procedures to divide complex code tasks into more manageable units.    

Procedures have start and end lines like this: 

Private Sub cmdBeep_Click()   

End Sub

The lines of code go between these two lines.  This procedure would run when the user clicks on the cmdBeep button

Naming Event Procedures

Each procedure is given a name.   The name of an event procedure is made up of the object name and the event name, separated by an underscore.  

For example:  cmdExit_Click refers to the Click event for the cmdExit button

Controls and their properties are named using dot notation:

For example:   cmdExit.Backcolor refers to the background colour property of the button named cmdExit

 


We are now ready to write some code in our program.

Add Your Code

Private Sub cmdBeep_Click()
     Beep
End Sub

 

Test your Program

Now your program is ready to be tested

Clicking the button caused a Click event to occur for the cmdBeep button.  

The computer recognised this and ran the cmdBeep_Click event procedure.     

 
Try to change the program so that the computer beeps when the user moves their mouse over the button instead of clicking it.

Hint: Look for MouseMove in the top right drop down box of the code window

Saving Your Project

Visual Basic Projects are always made up of more than one file.  Every project has one project file (stored with .vbp filename extension).  This file lists all the separate files that make up the project.  Each form in your project is stored as a separate file with the extension .frm  It makes sense to create a new folder for each project that you make and store all its files together.  This makes it easier to find them later on if you want to change something.

Projects and forms need to be saved separately.  

You can save a single form as you create it by choosing File > Save Form  (This will not save your project or any other forms you might have)

If you save your project, after it is saved you will then be asked if you want to save any forms you have open as well.  

You need to save both your project and any forms separately.

You may want printouts of the work you do in your projects.  There are three ways you can do this.  When you click File > Print, you will get  3 print choices:
  • Form Image - Prints your form exactly as it appears on screen
  • Code - Prints only your BASIC code
  • Form as Text - Prints the property values of all the forms and objects that make up your user interface.