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


Step Nine - Putting it all together

That really is all there is to writing Visual Basic programs - control objects, events and properties.


When you create a Visual Basic application you will spend some time creating the look of the form complete with button boxes etc. and some more time defining properties and event routines.

At the moment it might seem difficult to believe that this is powerful enough a way of working to allow you to create full Windows applications. All I can say is that it is!

The remainder of this chapter is a short example which you might not fully understand but it will give you a good idea how things work in moving from a small example to something that works.

You can also download the complete example.

Program - A Lunar Lander

To show you just how powerful the Visual Basic approach is let's build a Lunar Lander program.

The first step is to paint a suitable space vehicle. To do this you need to use Windows Paint or any other painting program you may have to paint a small - around 80x80 pixels - spacecraft. (The graphic below is included in the project download.)



Save this as LAND1.BMP in a suitable directory.

Next start Visual Basic and create a new Windows Forms project called Lander.

When the project has opened place a PictureBox control on the default form. The PictureBox control icon looks like a small painting of a mountain scene:




The PictureBox displays a bitmap image file on the form. You need to specify the location of the bitmap. Use the Properties window to set the Image property to the images that are going to be displayed.

In this case the simplest thing to do is to is to click on the Image property. In the dialog box that appears select the Project resource file option. Finally click the Import button and navigate to the location of the LAND1.BMP file. When you click OK the image file is copied into a resource directory in your project.This method of using a  bitmap file ensures that the file is included in the project and finished application:




After this you should see your picture of a spacecraft appear in the PictureBox control. To make sure that you see all of the image use the Properties window to change the SizeMode property to AutoSize.

Also change the PictureBox's name to Lander and set the Form's BackColor to White so that the Lander image blends in:




The next step is to find a way to animate the lander's picture. This depends on two ideas, both of which are new and will be discussed in greater detail in later chapters  - for now just get the feel for it all.

The first idea is to use a Timer object. A timer doesn't appear on the form when you run the program but it does have an associated event that occurs at a regular intervals.

That is, a timer ticks and you can make things happen on each tick.

Place a timer on the form using the timer tool - it looks like a stop watch. In the Designer it doesn't appear on the form but in an area at the bottom of the screen reserved for controls that are invisible at run time:



Now define the Timer's Interval property to be 00, i.e. one tick every 100 milliseconds also set the Timer's Enabled property to True - this starts the Timer ticking as soon as you run the program. Notice that you can select the timer by clicking on it in the Designer just like any control.

In the same way double clicking on the Timer takes you to its main event handler - the tick event handler. Will in the code editor  define the timer's only event routine as:

Private Sub Timer1_Tick(
  ByVal sender As System.Object,
  ByVal e As System.EventArgs)
              Handles Timer1.Tick
 Lander.Top += 1
 End Sub

What is this all about?!

Well the image of the lander has a property called Top which determines where the top of the image control is on the form. The line:

Lander.Top += 1

adds one to the lander's vertical position each time the timer ticks.

If you run this program you will see the lander slowly move down the screen with each tick of the timer.

If you want to investigate what is happening further, change the timer interval to 1000, i.e. one tick per second.

<ASIN:0132152134 >

<ASIN:1890774588 >

<ASIN:0470532874 >

<ASIN:047050224X >

Last Updated ( Thursday, 07 April 2011 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.