previous page main page next page

To summarise, because it's important that you appreciate what's going on so far:
 

1. the user moves the scroll bar to select a value
2. the value is made available to the program by using the scroll bar's Value property
3. we're going to assign that value to a variable called Miles
4. we display the value of Miles in a text box
 

Firstly, we declare the two variables in the General Declarations section like this:
 


 

  • type in the code, remember to save your work
     

Some points to note here:

What we're doing is simply telling the program: "Hey, we're going to use a couple of variables called Miles and Kilometers. The first one, Miles, is definitely an integer; the second one, Kilometers, we're not too sure just now, so we'll leave it vague for the time being." You'll meet other types of variables later on in the course.

The phrase 'Option Explicit' appears because, earlier, we asked you to make sure that the Require Variable Declaration was ticked as one of the optional settings. What this does is to ensure that Visual Basic will complain if you try to use a variable without declaring it first. This is good practice, as you'll see later.

Turning to the code for the hsbMiles scroll bar, we have:
 

Private Sub hsbMiles_Change()

Miles = hsbMiles.Value
txtMiles.Text = Miles

End Sub
 

You'll notice that this is the Change event and so the code is executed whenever the user changes the Scroll Bar setting. This, of course, is exactly what we want and you can see that Miles takes on the value that the scroll bar is set to and then the Text property of the txtMiles text box, in turn, is assigned the value of Miles
 

  • type in the code
  • run the program
     

Continued...
 

previous page main page next page ©  ePublish Scotland  1999