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


ArrangeControls sets the scroll bar’s LargeChange property to NumBars. This makes the histogram display a whole new set of values when the user clicks between the scroll bar’s slider and one of its arrow buttons.

Private Sub ArrangeControls()
Const GAP = 60

Dim wid As Single
Dim hgt As Single
Dim tp As Single
Dim i As Integer
Dim x As Single

    If loading Then Exit Sub

    ‘ See how wide the control needs to be.
    wid = NumBars * (Bar(0).Width + GAP) - GAP
    Width = wid + (Width - ScaleWidth)

    ‘ See if we will need the scroll bar.
    If NumBars >= NumValues Then
        ‘ We don’t need the scroll bar.
        ‘ Make the bars as tall as possible.
        hgt = ScaleHeight
    Else
        ‘ We do need the scroll bar.
        ‘ Place the scroll bar.
        tp = ScaleHeight - ScrollBar.Height
        ScrollBar.Move 0, tp, ScaleWidth

        ‘ Leave room for the scroll bar.
        hgt = ScrollBar.Top - GAP
    End If

    ‘ Arrange the gauges.
    wid = Bar(0).Width
    x = 0
    For i = 1 To NumBars
        Bar(i).Move x, 0, wid, hgt
        x = x + wid + GAP
    Next i

    ‘ Make the bars visible.
    For i = 1 To NumBars
        Bar(i).Visible = True
    Next i

    ‘ Make the scroll bar visible or not as appropriate.
    If NumBars >= NumValues Then
        ScrollBar.Visible = False
    Else
        ScrollBar.Visible = True

        ‘ Set the scroll bar’s properties.
        ScrollBar.Max = NumValues - NumBars
        ScrollBar.LargeChange = NumBars
    End If
End Sub

Scrolling Data

When the scroll bar’s value changes, its event handlers invoke the ShowValues subroutine. This routine sets the Value properties of the gauges so they display the appropriate data values.

Private Sub ScrollBar_Change()
    ShowValues
End Sub

Private Sub ScrollBar_Scroll()
    ShowValues
End Sub

Private Sub ShowValues()
Dim off As Integer
Dim i As Integer

    off = ScrollBar.Value
    For i = 1 To NumBars
        Bar(i).Value = Value(i + off)
    Next i
End Sub

Delegating to a Control Array

The histogram control delegates many of its properties to the gauge controls it contains. These properties are all handled similarly, so only the TicWidth property procedures are described here.

While the gauge control with index 0 is never visible, it is used to represent the properties of the other gauges. Whenever a gauge property value is changed, it is changed for all of the loaded gauge controls, including the one with index 0. This allows the visible controls to update their appearances. It also keeps the control with index 0 up to date so the delegated property get procedures can manage its property values.

Public Property Let TicWidth(val As Long)
Dim i As Integer

    For i = 0 To NumBars
        Bar(i).TicWidth = val
    Next i

    PropertyChanged “TicWidth” ‘ Tell VB.
End Property

Public Property Get TicWidth() As Long
    TicWidth = Bar(0).TicWidth
End Property

Supporting Indexed Properties

The histogram control represents many data values. To allow client programs to read and set all of these values, the histogram control’s Value property is indexed.

Creating indexed property procedures is simply a matter of including an extra parameter. The first argument in the following Value property procedures is the index of the value to be returned or set.

Notice that these procedures check to make sure the index is within the range of valid values. This is extremely important. If either procedure tries to access an array value that does not exist, such as entry 0, the code will generate a “Subscript out of range” error.

Private the_value() As Long

Public Property Get Value(i As Integer) As Long
    If i < 1 Or i > NumValues Then
        Value = 0
    Else
        Value = the_value(i)
    End If
End Property

Public Property Let Value(i As Integer, val As Long)
    If i < 1 Or i > NumValues Then Exit Property

    the_value(i) = val

    ‘ Redisplay the values (we don’t need to
    ‘ rearrange the controls).
    ShowValues

    PropertyChanged “Value” ‘ Tell VB.
End Property

This is not the only way a control can support multiple values for a single property. One alternative would be to provide property procedures that take as a parameter an array of values. The property let procedure would copy the values from the array into the control’s local data structures. The property get procedure would use the control’s local values to fill in the entries in the array. The procedures could also take extra arguments to allow the client application to fill in ranges of values rather than all of the possible values. Setting many values with a single procedure call will generally be faster than setting the same number of values one at a time.

Raising Indexed Events

When the user modifies a value using one of the gauges, the histogram control raises an event to notify the client application. It passes as a parameter to the event handler the index of the value modified. Then the application can take appropriate action such as saving the new value to a file.

To create an event with a parameter, the program must declare the parameter when it declares the event. The following code shows how the histogram control declares the ValueChanged event:

Public Event ValueChanged(Index As Integer)

Once the event is declared, raising it is easy. The program simply uses the parameter in the RaiseEvent statement. The following code shows how the histogram control responds to a Change event generated by one of its gauge controls:

Private Sub Bar_Change(Index As Integer)
Dim i As Integer

    ‘ Calculate the index of the changed value.
    i = ScrollBar.Value + Index

    the_value(i) = Bar(Index).Value

    ‘ Raise the value changed event.
    RaiseEvent ValueChanged(i)

    PropertyChanged “Value” ‘ Tell VB.
End Sub

Summary

For most purposes, ActiveX controls behave just as other controls. In particular, a program can use ActiveX controls to build other ActiveX controls. Component controls can be loaded using Visual Basic’s LOAD statement. Properties can be assigned to these controls just as with simpler controls such as labels and text boxes.

Because ActiveX controls behave much like other controls, a developer can build complex controls such as the Histogram control in incremental stages. By itself, the BarGauge control is relatively simple. Using BarGauge controls to build the Histogram control is even simpler.


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.