Hello VB
Article Index
Hello VB
Running the program
A Lunar Lander
Event routines and liftoff!

Step Five - Running your program

Being able to draw a button is fun at first but after you have seen buttons of all sizes you start to want to do something more. If you simply select the Run button, the green triangle below the menu then, after a short delay, the current program will start executing:

 

run

 

There are other ways to give the run command - press F5 or select the menu items Debug, Start Debugging.

It doesn't matter how you give the run command the result is the same - your program will be run and you will see a form appear outside of the Designer that you used to create it and behave just like any other application window. It will also have a button that you can click:

 

firstprog

 

Your button is fully functional in the sense that you can press it - but off course nothing else happens. To stop the program simply click on the Stop button, use Ctrl+Alt+Break or the menu command Debug, Stop Debugging:

 

stop

 

Step Six - Events

Obviously you need to add some functionality to your first Visual Basic program.

What is needed is a definition of what should happen when you click on the button. A "click" is an example of a Visual Basic event.

All Visual Basic objects respond to a range of standard events such as clicks, double clicks and so on. The default response is to an event is generally to do nothing.

It is up to you to associate a set of commands with each event that achieves something.

To make something happen in response to an event you have to write an event handler. To see an example of an event handler for the button object all you have to do is double-click on it - this makes the code view window appear with a suitable event handler ready for you to edit:

 

codeeditor

 

Now you have two tabs at the top of the editing area and you can select the design view or the code view by clicking on them. You will have to swap from one to the other quite often while creating an application. The design view lets you arrange controls on the form and the code view lets you type in commands that make things happen.

Step Seven - Working with commands

If you look more carefully at the code view window you will see that there two drop-down list boxes - one shows Button1 and the other Click:

 

lists

 

From the first list box you can select which object you are working with and from the other you can select the particular event. In this case it is the Click event for Button1. You can pull down the list on the right to see what sort of events an object can respond to.

To define what happens when an event occurs you simply type commands into the code window between the Sub and End Sub command that appear in the code window. Of course you need to know what sort of commands you can use. Finding out what command you can use is what mastering Visual Basic is all about.

As a simple example -  the command BEEP makes your machine make a default "alert" noise. If you add it to the event routine and run your program - then as long as you don't have the volume turned down, have speakers connected etc, then you will hear a noise when you click the button..After you have added the command the event handler should read:

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

Step Eight - Properties

Objects and events are two of the main principles of Visual Basic programming. The third is the use of properties.

All objects have a range of properties that can be viewed and altered using the Properties Window (at the right of the editing area).

 

properties

 

You can scroll down the list to see what sorts of properties are available - some, like Size, make immediate sense but others, like Tag need some explanation.

If you want to change a property you simply select it and type in a new value or select from the alternatives presented.

For example, ForeColor sets the color used to render the button's caption. If you select its current value you can pick a new color from the palettes that are presented to you.

You can change the properties of any control you have placed  on the form in this way.

<ASIN:143022455X >

<ASIN:0073517259 >

<ASIN:0521721113 >

<ASIN:0136113400 >