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


Managing Bar Gauge Properties

Like the alarm control’s properties, the properties of the bar gauge are saved and read from a project’s form file by the WriteProperties and ReadProperties event handlers. These routines are very similar to those used by the alarm control, so they are not shown here. You can find them in the Ch15 directory on the compact disk.

The bar gauge’s Enabled property works just as it does for the alarm control, so it is not described here. Several other properties are more interesting; they are explained in the following sections.

Min, Max, KeyChangeAmount, and Value

The bar gauge’s Min, Max, KeyChangeAmount, and Value properties are all long integers, and they are all handled similarly. Each is stored in a variable that is private to the control. For instance, the variable that stores the value for Min is declared as follows:

Private the_min As Long

The property procedures for these values are also quite similar. The property get procedures simply return the value of the private variable. The property let procedures store the new value in the control’s private variable and use the PropertyChanged subroutine to notify Visual Basic that the property value has been modified. They also invoke the control’s Paint subroutine. This makes the control redraw itself so that it is displayed using the new property values. The Paint subroutine is described in the section, “Drawing Bar Gauge,” in this chapter.

The following code shows the property procedures for the Min property. The procedures for Max, KeyChangeAmount, and Value are very similar.

Public Property Get Min() As Long
    Min = the_min
End Property

Public Property Let Min(val As Long)
    the_min = val

    PropertyChanged "Min"   ‘ Tell VB.
    Paint   ‘ Redisplay the gauge.
End Property

BackColor and ForeColor

The UserControl object provides ForeColor and BackColor properties, so the bar gauge can delegate ForeColor and BackColor to the UserControl object. The following code shows how the control delegates the ForeColor property let procedure using the UserControl’s ForeColor property. The Picture2D and Picture3D objects are not important right now. They are explained later in the section, “Appearance.”

Public Property Let ForeColor(clr As OLE_COLOR)
    UserControl.ForeColor = clr
    Picture2D.ForeColor = clr
    Picture3D.ForeColor = clr
    PropertyChanged "ForeColor" ‘ Tell VB.
    Paint   ‘ Redisplay the gauge.
End Property

To make the bar gauge’s foreground and background colors match the rest of the application, the bar gauge initializes these values using ambient values, as described in Chapter 14. The following code shows how InitProperties initializes the control’s properties.

InitProperties uses the Boolean variable loading to defer redrawing the control until all the properties are loaded. Most of the bar gauge’s property let procedures make the control redraw itself when they are invoked. Using the loading variable prevents the control from redrawing itself unnecessarily.

Private Sub UserControl_InitProperties()
    loading = True
   
    Appearance = dflt_Appearance
    BackColor = Ambient.BackColor
    ForeColor = Ambient.ForeColor
    Max = dflt_Max
    Min = dflt_Min
    Style = dflt_Style
    Value = dflt_Value
    KeyChangeAmount = dflt_KeyChangeAmount

    Enabled = dflt_Enabled
 ‘ Redraw now.
    loading = False
    Paint
End Sub

Because ForeColor and BackColor are colors, the control declares all the variables dealing with them to be of type OLE_COLOR. For example, the ForeColor property get procedure shown here is declared to return a value of type OLE_COLOR.

Public Property Get ForeColor() As OLE_COLOR
    ForeColor = UserControl.ForeColor
End Property

Declaring a color property to be of type OLE_COLOR allows Visual Basic to present the color properly in the Properties window at design time. The entry for ForeColor is shown with a small sample of the color selected and the color value in hexadecimal. If the application designer clicks on the drop-down arrow to the right of this value, a standard color selection palette appears.

It is common, particularly in older programs, to declare color values as long integers. If the control did this, the Properties window would not display the color sample. Instead, it would show the color’s value in a practically meaningless decimal form and would not provide the standard color selection palette.

Style

Declaring property procedures using the correct data type helps the Properties window display values correctly for data types other than colors, too. Declaring the Enabled property to be of type Boolean makes the Properties window display its values as true or false.

The bar gauge’s Style property is an enumerated type. It can take only one of four values, each of which has a special meaning. The values are defined in the control class using the following public enum statement. The control keeps track of its style in the private variable the_style, declared as type GaugeStyle.

Public Enum GaugeStyle
    gsLeftToRight
    gsRightToLeft
    gsBottomToTop
    gsTopToBottom
End Enum
Private the_style As GaugeStyle

The Style property procedures shown here are declared using the enumerated data type GaugeStyle.

Public Property Let Style(val As GaugeStyle)
    the_style = val

    PropertyChanged "Style" ‘ Tell VB.
    Paint   ‘ Redisplay the gauge.
End Property

Public Property Get Style() As GaugeStyle
    Style = the_style
End Property

Using an enumerated type for the property procedures allows the Properties window to list the choices that are available. The window displays the enumerated value that is currently selected. Clicking on the value’s drop-down arrow makes Visual Basic present a list of the allowed choices.

Using an enumerated type also has the advantage that it allows application designers to specify values for this property using the enumerated constants. The following code would make a bar gauge use the bottom-to-top vertical style.

BarGauge1.Style = gsBottomToTop

The application designer can find a list of the allowable values for an enumerated property using the Object Browser. Figure 15.2 shows the Object Browser with the Style property selected.

The last part of the description includes text that describes the purpose of the property. In this case, the text reads, “Orientation and direction (left-to-right, etc.).” This text is set by opening the control’s design window and selecting the Procedure Attributes command from the Tools menu. After selecting the property from the Name list, the text to be displayed by the Object Browser should be entered in the Description box.

Clicking on the highlighted text GaugeStyle in the Object Browser makes the Browser display the definition of the type GaugeStyle. Figure 15.3 shows the Object Browser displaying this definition.


FIGURE 15.2  Examining the Style property using the Object Browser.


FIGURE 15.3  GaugeStyle values shown by the Object Browser.


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.