VB Logo

Tutorial: Vertical & Horizontal Scroll Bars


The objective of this tutorial is to learn how to use vertical and horizontal scroll bars. At the end of this tutorial you should be able to:



Introduction

Scroll bars can be useful in a number of ways. Vertical scroll bars are commonly seen as a means of manipulating text, where all the text to be viewed cannot be displayed simultaneously. However, both types of scroll bars can be used for purposes other than traditional scrolling, for example, they may be used as an interactive means of setting values such as mouse tracking speed, volume level, temperature or speed.

Use VB Help to find out more about these controls (select one of the controls from the toolbox and press F1; F1 gives context sensitive help).



Task

  1. Write a simple application as follows:

    Interface:


    Behaviour:

    Labels at either end of the scroll bars show their minimum and maximum values (set at design time).

    A status bar at the bottom of the form to display current height and speed. The values in the status bar will be updated when the two scroll bars change.

    Hint: This was the code behind the horizontal scroll bar. The ‘&’ is a concatenation operator which allows us to ‘glue’ string elements together. Notice that the constant parts of the composite string appear between double quotation marks, such a portion is known as a string literal (it will be displayed exactly as it appears). Variables such as hsbSpeed.Value (the current value of the horizontal scroll bar) can be included in a string - these are evaluated by VB and the result inserted.

      Private Sub hsbSpeed_Change ()
          StatusBar1.Panels(2).Text = "Speed:" & hsbSpeed.Value
    
      End Sub
    

  2. Use scroll bars to manipulate screen objects, in this example shapes are used.

    Experiment with the shape control . Find out how to change the shape through the properties window.

    Interface:

    Behaviour:

    The three command buttons are used to change the shape of the object displayed.

    The vertical scroll bar controls the height of the object and the horizontal scroll bar controls its width.

    When you create the shape, experiment with width and height to determine sensible maximum values. The minimum values of the scroll bars should be set to the same value as the starting height and width of the object.

    Note: the circle, of course, will not change height and width independently.

    Hint: This application only has ONE line of code behind each of the five interactive controls!

    Of course you can interactively alter the properties (such as size) of screen objects in other ways, for example via menus, command buttons, check boxes etc. - do some experiments to change color and fontsize by different methods.




Tutorial 6 (Creating and Using Arrays of Controls)
Menu
Tutorial 8 (Animation and Graphics)