previous page main page next page

Well that seems OK so far. How about changing the number of miles into kilometers? You need to add a couple of lines of code as follows:
 

Private Sub hsbMiles_Change()

Miles = hsbMiles.Value
txtMiles.Text = Miles
Kilometers = 1.6 * Miles
txtKilometers.Text = Kilometers

End Sub  

Like many other programming languages, Visual Basic uses the character '*' to mean 'multiply'.

  • run the program now and it should do pretty well what we wanted it to
     

...pretty well, that is, but not quite all. Have you spotted the problem? If not, try this:
 

  • run the program and click and drag the thumb of the scroll bar
     

The value is only displayed when you release the thumb. It would be much better if we could update the values as the bar is being scrolled. No problem. Look for the Scroll event in the hsbMiles section and add exactly the same code that appears for the Change event - copy and paste it to get this:
 

Private Sub hsbMiles_Scroll()

Miles = hsbMiles.Value
txtMiles.Text = Miles
Kilometers = 1.6 * Miles
txtKilometers.Text = Kilometers

End Sub
 

  • run the program and check that it works as we intended
     

So now you've used variables. You know how to declare them and you've found a couple of ways to assign them a value. You've also learnt that sometimes you need to attach code for more than one event for the same control.

Traditionally, programmers have thought about programs in terms of input, process and output. In other words you get information, you do something with it and then you output the results to a screen or printer. This view has changed a little but is still relevant. The program you've just been using gets input from the scroll bar, processes it by calculating the number of kilometers and then outputs the results to a text box.
 

previous page main page next page ©  ePublish Scotland  1999