previous page main page next page

You'll see that this is really in four sections and we've put a comment line for each one. The first part should be familiar to you and simply assigns the values that the user has set to each of the three variables MarkA, MarkB and MarkC.

The second part
 

Average = (MarkA + MarkB + MarkC) / 3
 

calculates the average. Again like most languages, Visual Basic uses a '/' character to mean 'divide'. The '>' character means 'is greater than'.

To print the average value on the label, we use
 

lblAverage.Caption = "average is " + Str(Average)
 

which assigns it to the Caption property. Note that we must use the Str function again to convert the numerical value into a string value and note also that the answer is printed to five decimal places when necessary. We'll return to the topic of formatting the output at a later time.

The section of code
 

If MarkA > Average Then
  imgA.Picture = LoadPicture("c:\vb\icons\misc\face02.ico")
Else
  imgA.Picture = LoadPicture("c:\vb\icons\misc\face01.ico")
End If
 

checks to see if the mark is greater than the average. If it is, then we use another function, called LoadPicture to assign the specified picture file to the Picture property of the image control. If it isn't, we assign the other picture instead. In order to clear the image box altogether, you could use a line of code like
 

imgA.Picture = LoadPicture("")
 

You will also note that we set the Stretch property of the image controls to True.
 

  • set the Stretch property to False and see what happens
  • try resizing the image controls and note the effect that the Stretch property has
     

Now we did say that this system is brutal. If the three marks are, say, 40, 40 and 40 then nobody passes. Why is this? The answer lies in the way we decided who is to pass. If the three marks are 40, 40 and 40 then the average is also 40 and, of course, no one has got a mark which is actually greater than 40. If what we meant was greater than or equal to 40 then the code would be slightly different and you would use something like
 

If MarkA >= Average Then
 

  • make the three changes necessary and run this new version
     

Hey, this is much better, now everyone can pass!

So now you know how to use images in your programs and you can alter them at run time. We'll note, in passing, that if you wanted other people to be able to use this program on their machines, then it isn't necessary for them to have copies of the images installed. When you make an execuatble copy of your program, any images are incorporated in it automatically.
 

previous page main page next page ©  ePublish Scotland  1999