![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
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 controls 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 programs 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 controls 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 TechniquesThe following list briefly describes new techniques demonstrated by the histogram control. The rest of the chapter explains these techniques in detail.
Managing Control ArraysThe 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 Basics Load command to create gauges with indexes 1, 2, and so on. A client application or the application designer can use the histograms 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
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 bars 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.
|
|
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.
|