home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


The NumValues property determines the number of data values the control will display. After this value is set, the application can set data values using the indexed Value property. The HistTest program uses the following code to initialize its 100 data values. The function used to calculate the values rises and falls in an interesting way.

Private Sub Form_Load()
Const PI = 3.14159265

Dim i As Integer

    Histogram1.NumValues = 100
    For i = 1 To 100
        Histogram1.Value(i) = 50 + 50 * _
            Sin(PI * i / 10) * _
            Sin(PI * i / 15) * _
            Cos(PI * i / 5)
    Next i
End Sub

If the histogram control’s Enabled property has the value true, then the user can interact with the gauges to modify the data at run time. To notify the client application when this occurs, the control provides a ValueChanged event. Whenever the user modifies a value, the control invokes the program’s ValueChanged event handler, passing it the index of the data value that was modified. The program can then take appropriate action, such as saving the modified value into a file.

To keep the code as simple as possible, the histogram control does not provide any other events. A reasonable addition would be a Scroll event that triggered when the user modified the control’s scroll bar value. This would inform the program that the data values displayed had changed. The program could then take actions such as updating a group of labels that displayed the values shown by the gauges as text.

Key Techniques

The following list briefly describes new techniques demonstrated by the histogram control. The rest of the chapter explains these techniques in detail.

•  Managing Control Arrays. When the histogram control’s NumBars property is changed, the control must load and arrange the correct number of gauges. This section explains how the histogram control uses a control array to manage the bar gauges.
•  Scrolling Data. Because the histogram control can contain more data values than it has gauges, it provides a scroll bar to allow the user to scroll through the data. This section tells how the control uses the scroll bar.
•  Delegating to a Control Array. Many of the histogram control’s properties are delegated to the gauges it contains. This section describes the methods the histogram uses to delegate properties to the bar gauges it contains.
•  Supporting Indexed Properties. The histogram control represents many data values. This section explains how the histogram provides indexed properties to allow client programs to read and set the data values.
•  Raising Indexed Events. The histogram control raises an event to notify the client application when the user changes a data value. This section shows how the control passes the event handler the index of the modified value.

Managing Control Arrays

The histogram control contains an array of gauge controls and a scroll bar. Figure 16.2 shows the control at design time. The vertical gauge control has Index property 0. When other gauges are needed, the histogram control uses Visual Basic’s Load command to create gauges with indexes 1, 2, and so on.

A client application or the application designer can use the histogram’s NumBars property to determine the number of bar gauges the control displays. Changing the number of gauges has several consequences. First, the control unloads any old gauge controls it no longer requires. If the user changes NumBars from 10 to 5, for example, five of the controls must be unloaded.

Next, the control loads any new gauges it needs. If the user increases NumBars from 3 to 10, the control creates seven new bar gauge controls.

After unloading and loading gauges as needed, the NumBars property let procedure saves the new number of controls loaded. It will need this number later if NumBars is changed again. Finally, the procedure invokes the ArrangeControls subroutine to arrange the gauges and the scroll bar.

Public Property Let NumBars(val As Integer)
Dim i As Integer

    ‘ There must be at least 1 gauge.
    If val < 1 Then Exit Property

    ‘ Unload bars no longer needed.
    For i = val + 1 To num_bars
        Unload Bar(i)
    Next i

    ‘ Load bars now needed.

    For i = num_bars + 1 To val
        Load Bar(i)
    Next i

    ‘ Save the new value.
    num_bars = val

    ‘ Arrange the controls.
    ArrangeControls

    PropertyChanged “NumBars” ‘ Tell VB.
End Property


FIGURE 16.2  The histogram control at design time.

Subroutine ArrangeControls aligns the gauges horizontally 60 twips apart. It begins by calculating the width needed to display all of the scroll bars. It then gives the UserControl that width.

Next, the routine compares NumBars and NumValues to see if the scroll bar is needed. If not, it sets hgt to be the total height available within the UserControl. The value hgt will be used later to set the height of the gauges. If the scroll bar is needed, the subroutine makes hgt smaller to leave room for the scroll bar.

ArrangeControls then places the gauges and makes them visible. If the scroll bar is needed, the routine positions it and makes it visible as well.

The subroutine finishes by setting the scroll bar’s Max and LargeChange properties. Max is set so the scroll bar can range from 0 to NumValues - NumBars. When the scroll bar has value i, the leftmost gauge displays the ith data value. When the scroll bar has value 0, the leftmost gauge displays the data value in position 1. When it has value Max, the leftmost gauge displays the value in position Max + 1. In that case, the rightmost gauge would display the value in position (Max + 1) + (NumBars - 1) = ((NumValues - NumBars) + 1) + (NumBars - 1) = NumValues. This makes sense. When the scroll bar value is as large as possible, the rightmost gauge displays the last data value.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.