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


When the form is reloaded, the properties of the controls contained in the form must be reloaded from the form file. Like WriteProperties, the UserControl’s ReadProperties event handler also uses a property bag object to manage control properties. The object’s ReadProperties function takes two parameters. The first is the name of the property to be loaded. The second is the default value that should be assigned to the property if no value was stored in the form file by the WriteProperties event handler.

The following code shows how the alarm control reads its properties from the form file. ReadProperties uses the same Boolean variable loading used by the InitProperties event handler to defer action until all of the properties are loaded. This prevents the control from taking action before all the properties have been reloaded.

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    ‘  Do not run while loading.
    loading = True
   
    AlarmDate = PropBag.ReadProperty("AlarmDate", dflt_AlarmDate)
    Enabled = PropBag.ReadProperty("Enabled", dflt_Enabled)

    ‘  Run now if appropriate.
    loading = False
    AlarmTimer_Timer
End Sub

Delegating Standard Properties

The UserControl object defines certain standard properties, such as BackColor, FillStyle, and Font. Rather than building these properties from scratch, an ActiveX control can delegate the duties needed to manage them. It does this by invoking the properties of the UserControl object.

For example, the BackColor property get procedure shown in the following code simply returns the value given by the UserControl object’s BackColor property.

This routine returns a value of type OLE_COLOR. The Visual Basic design environment uses property procedure types to determine how it should present values in the Properties window. Using the OLE_COLOR data type for the BackColor property tells the environment to use standard color selection for BackColor in the Properties window.

Property Get BackColor() As OLE_COLOR
    BackColor = UserControl.BackColor
End Property

Similarly the following property let procedure sets the UserControl’s BackColor property to a new value. It also uses the PropertyChanged subroutine to notify the environment that the value has been modified.

Property Let BackColor(new_color As OLE_COLOR)
    UserControl.BackColor = new_color
    PropertyChanged "BackColor"
End Property

To support standard properties such as BackColor, the event handlers for the UserControl’s InitProperties, ReadProperties, and WriteProperties events must also be modified. The InitProperties event handler should set a default value for BackColor. The ReadProperties and WriteProperties event handlers should read and write the value of BackColor to the form file just as they do for other property values.

This is all the code the control needs to support the BackColor property. Because the UserControl object provides support for BackColor, it changes the control’s background color whenever necessary.

Property IDs

Each property and event defined by an ActiveX control has an identification number called a procedure ID. Certain key properties and events have special IDs defined by the ActiveX standard. In some situations, Visual Basic uses these properties and events by referring to their procedure IDs rather than their names. If an ActiveX control is to behave in the same way other controls do, its standard properties and events must have the correct procedure IDs.

The steps for assigning procedure IDs to standard properties and events are listed below:

1.  Open the control’s object window.
2.  Select the Procedure Attributes command from the Tools menu.
3.  Click the Advanced >> button.
4.  Select a property or event in the Name box.
5.  Scroll through the Procedure ID box and select the appropriate procedure ID.

Standard IDs should be assigned whenever they are defined. Standard IDs for methods are defined for click, key, mouse, and error events. Standard IDs are defined for many properties including Appearance, BackColor, BorderStyle, Enabled, FillColor, and Font.

The Enabled Property

The Enabled property behaves differently than other properties supported by the UserControl object. This property is not supported unless the control provides Enabled property procedures and Enabled has been given the standard procedure ID. If these conditions are not met, the control will not behave as other controls do.

Raising Events

Before a control can cause an event to trigger, it must declare the event. The alarm control uses the following Event statement to declare the Alarm event:

Public Event Alarm()

Later, when the control needs to trigger the event, it uses a RaiseEvent statement, like this one:

RaiseEvent Alarm

The alarm control uses a normal timer control, named AlarmTimer, to wait for the scheduled alarm time. A timer control’s Interval property specifies the time between the control’s Timer events in milliseconds. The Interval property can hold values up to 65,535, so the Timer events can be separated by, at most, roughly 65 seconds.

AlarmTimer’s event handler begins by checking the value of the Boolean variable loading. If its value is true, the program is in the middle of initializing or reloading the control’s properties; AlarmTimer_Timer takes no action.

Next AlarmTimer_Timer checks to see how far in the future the alarm time is. If the alarm time is less than one second away, the event handler disables AlarmTimer, disables the ActiveX alarm control, and raises the Alarm event.

Otherwise, if the alarm time is less than one minute away, the event handler resets AlarmTimer’s Interval property so that the next Timer event will occur when the alarm is scheduled.

Finally, if the alarm time is more than one minute away, the code sets AlarmTimer’s Interval property to 60,000 so that the next Timer event will occur in roughly 60 seconds. When that event occurs, the event handler will again see how far in the future the alarm is. This process continues until the alarm time arrives.

   Private Const LONG_WAIT = 60000
          :
      Private Sub AlarmTimer_Timer()
      Dim remaining As Long
      
          ‘  Do not start if we’re loading.
          If loading Then Exit Sub
         
          ‘  See how many seconds until alarm time.
          remaining = DateDiff("s", Now, alarm_date)
         
          If remaining < 1 Then
          ‘  Close enough. Raise the alarm.
          AlarmTimer.Enabled = False
          Enabled = False
          RaiseEvent Alarm
      ElseIf remaining < LONG_WAIT Then
          ‘  It’s less than one minute. Set the
          ‘  Interval property to wait for the
          ‘  required number of seconds.
          AlarmTimer.Interval = remaining * 1000
          AlarmTimer.Enabled = True
      Else
          ‘  It’s more than one minute. Set the
          ‘  Interval property to wait 60 seconds.
          AlarmTimer.Interval = LONG_WAIT
          AlarmTimer.Enabled = True
      End If
  End Sub

Summary

The Alarm control is fairly simple. It has no visible interface at run time, provides just one nonstandard property, AlarmDate, and supports a single new event, Alarm. Despite its simplicity, the Alarm control performs all of the major tasks required of any ActiveX control. It initializes its property values in the InitProperties event handler, saves values in the WriteProperties event handler, and reloads them in ReadProperties. Other ActiveX controls, such as the BarGauge control described in Chapter 15, provide more complex functionality, but they all perform these same three basic tasks.


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.