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


Apart from its appearance, the main difference between the dial gauge and the others is in how it determines the value selected by mouse events. The previously described gauges see how far across the control the mouse has moved and then calculate the appropriate value between the control’s Min and Max values.

The dial gauge’s ComputeValue function is only a little more complicated. First it calculates the angle between the horizontal and the line connecting the point selected to the center of the dial. This angle is shown in Figure 15.6.

ComputeValue then sees where this angle lies between the right horizontal angle 0 and the left horizontal angle P (3.14159265). It uses the result to determine the value selected.

For example, if the angle is 0.79 radians, it is roughly one-quarter of the distance from the angle 0 to the angle P. If the control has the right-to-left style, the value selected is one-quarter of the distance from the control’s Min value to its Max value.

Private Function ComputeValue(the_canvas As PictureBox, _
    X As Single, Y As Single) As Long

Dim fract As Single
Dim val As Long
Dim wid As Single
Dim hgt As Single
Dim x0 As Single
Dim y0 As Single
Dim dx As Single
Dim dy As Single
Dim theta As Single

    ‘ No range. Pick the_min.
     If the_min >= the_max Then
        ComputeValue = the_min
        Exit Function
    End If
   
    ‘ See at what fraction of the way through
    ‘ the range the point lies.
    wid = the_canvas.ScaleWidth
    hgt = the_canvas.ScaleHeight
    x0 = wid / 2
    y0 = hgt - 1
    dx = X - x0
    dy = y0 - Y
    theta = Arctan2(dx, dy)
    ‘ Convert that into Dial units.
    Select Case the_style
        Case gsLeftToRight
            fract = (PI - theta) / PI
     
        Case gsRightToLeft
            fract = theta / PI
     
        Case Else
            ComputeValue = the_min
            Exit Function
    End Select

    ‘ Compute the result.
     val = the_min + _
        (the_max - the_min) * fract
      
    ‘ Keep the result in range.
    If val < the_min Then
        val = the_min
    ElseIf val > the_max Then
        val = the_max
    End If
      
    ComputeValue = val
End Function


FIGURE 15.6  Calculating a selected point’s angle.

A Combined Gauge

The BarGauge, TicGauge, PicGauge, WidGauge, and Dial controls have much in common. To handle similar tasks, such as setting the Min and Max properties, these controls have identical copies of the same code. This redundant code takes up a great deal of space.

The Gauge.VBG project group in the Ch15\Combined directory defines a single gauge control that is a combination of all of the others. Its Style property determines how the control draws itself and how it calculates new values selected by the user. Style is a simple enumerated value with the following definition:

Public Enum GaugeStyle
    styleBar
    styleDial
    stylePic
    styleTic
    styleWid
End Enum

The new gauge control includes all of the properties required by each of the five different gauge types. These properties are handled exactly as they were before.

By creating a new control that can behave like each of the five others, the code that is duplicated among them is removed. For example, the new control has only one copy of the Min property let procedure rather than five separate copies.

This space saving is shown by the size of the compiled controls’ .OCX files. The .OCX file for the original BarGauge control is 29 KB. The .OCX for the four other separate gauges occupies 100 KB so the combined size of all five controls is almost 130 KB. The size of the new, combined .OCX is only 63 KB—less than half the size of the five separately.

Working with a single, combined control is convenient, but it is not always the best approach. In a Web-based application such as those described in Chapters 17, 18, and 19, users will need to download the .OCX file across a relatively slow network link. For some users, these links can be slow enough that a difference of a few kilobytes can have a fairly large impact. If the application truly needs all the different gauge control styles, it is better to download the combined control because it is smaller than the five separate controls.

On the other hand, if the application can be rewritten so that it uses only one of the five separate controls, it would be better to download only that control rather than the more elaborate combined version. The application would lose some variety, but the performance gain might be worth it.

Summary

The BarGauge control demonstrates several important interface techniques. It uses two picture boxes so it can provide both two- and three-dimensional appearances. These picture boxes allow it to provide design time, and even run time, access to a property that is normally unavailable to the designer and end user.

The BarGauge displays a data value. It uses mouse and keyboard events to allow the user to manipulate the value using the mouse or the arrow keys. With only minor changes to its drawing routine, it can provide a variety of different looks. These features give the BarGauge a much richer user interface than the Alarm control described in Chapter 14.


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.